@moonpay/auth-sdk
v0.1.48
Published
The MoonPay Authentication SDK
Downloads
785
Maintainers
Keywords
Readme
MoonPay Authenticate SDK
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.
Getting Started
1. Install the dependencies
To install the dependencies, run the following command in the root directory:
npm install
2. Run the Authenticate SDK
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
3. Connect to the MoonPay Authenticate SDK
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.
For local environment
- 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',
});
For production environment
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',
});
4. Implement Login and Logout
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.