@moonpay/auth-sdk
TypeScript icon, indicating that this package has built-in type declarations

0.1.48 • Public • Published

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

  1. In this repo, run the Authenticate package:
npm run auth:dev
  1. In Moonpay-api run the public api:
npm run dev:public-api
  1. 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.

Readme

Keywords

none

Package Sidebar

Install

npm i @moonpay/auth-sdk

Weekly Downloads

591

Version

0.1.48

License

MIT

Unpacked Size

443 kB

Total Files

8

Last publish

Collaborators

  • moonpay-engineering
  • moonpay-security
  • sbsto
  • ihell
  • tomislavhrenmp
  • npmitservices
  • jpulsifer-moonpay
  • vfaramond