The MoonPay Authenticate SDK is a library that provides a simple way to handle user authentication for MoonPay services. It allows you to easily integrate MoonPay's authentication flow into your application, providing a seamless experience for your users.
To install the dependencies, run the following command in the root directory:
npm install
Run the command in the packages/auth-sdk directory:
To run the project in dev mode:
npm run dev
To build the project:
npm run build
To connect to the MoonPay Auth SDK, you can either connect to local environment, or to production environment on auth.moonpay.com. You can use the test publishable key for testing purposes.
- In this repo, run the Authenticate package:
npm run auth:dev
- In Moonpay-api run the public api:
npm run dev:public-api
- Configure the SDK:
const sdk = new MoonPayAuthSDK('pk_test_123', {
devApiUrl: 'http://localhost:4000',
devAuthDomain: 'http://localhost:5001',
});
Just set the SDK configuration to the remote URL:
const sdk = new MoonPayAuthSDK({
apiKey: 'pk_test_123',
devDomain: 'https://auth.moonpay.com',
authType: 'EMAIL_OTP',
});
You can now use the SDK methods to manage user authentication. Here's a simple example of how to handle login and logout:
import { useState, useEffect } from 'react';
// State to track user login status
const [isLoggedIn, setIsLoggedIn] = useState(false);
// Initialize the SDK
useEffect(() => {
const loadSdk = async () => {
await sdk.init();
};
loadSdk();
}, []);
// Handle user login
const handleLogin = async () => {
if (sdk) {
sdk.showUI();
let authResult = await sdk.login();
// Handle the authentication result
if (authResult.success) {
setIsLoggedIn(true);
} else {
setIsLoggedIn(false);
}
}
};
// Handle user logout
const handleLogout = async () => {
if (sdk) {
let logoutResult = await sdk.logout();
// Handle the logout result
if (logoutResult.success) {
setIsLoggedIn(false);
}
}
};
See the extensive docs and examples here.