@launchlense-ai/authmiddleware-react

1.0.0 • Public • Published

AuthMiddlewareService

AuthMiddlewareService is an npm package for managing authentication via various API endpoints. It provides methods for login, OTP initialization, verification, and authorization.

Installation

To use AuthMiddlewareService in your React project, install it via npm:

npm install @launchlense-ai/authmiddleware-react

Usage

Get API Access keys

  1. Login/Signup to https://portal.amw.launchlense.tech.
  2. Create a new Project.
  3. You will get a access token as soon as you create a project. (!!!Do not loose the access token!!!)

Import the Service

First, import the AuthMiddlewareService class into your React component:

import AuthMiddlewareService from 'AuthMiddlewareService';

Initialize the Service

Create an instance of AuthMiddlewareService and initialize it with your access key:

const authService = new AuthMiddlewareService();
authService.initAuthMiddleware('your-access-key-here');

Replace 'your-access-key-here' with your actual access key.

API Methods

1. loginWithEmail

Logs in a user with email and password.

authService.loginWithEmail(
  'test@example.com',
  'password123',
  (response) => {
    console.log('Login successful:', response);
  },
  (error) => {
    console.error('Login failed:', error);
  }
);

Parameters:

  • email - The user's email address.
  • password - The user's password.
  • onSuccess - Callback function to handle a successful response.
  • onError - Callback function to handle an error response.

2. initLogin

Initializes the login process and optionally supports MFA.

authService.initLogin(
  'contact_number_or_email',
  6,
  (response) => {
    console.log('Initialization successful:', response);
  },
  (error) => {
    console.error('Initialization failed:', error);
  },
  true, // isMfa
  ['SMS', 'Email'], // mfaTypes
  'otp' // authType
);

Parameters:

  • contact - The user's contact number or email.
  • otpLength - The length of the OTP.
  • onSuccess - Callback function to handle a successful response.
  • onError - Callback function to handle an error response.
  • isMfa - Whether multi-factor authentication is enabled (default is false).
  • mfaTypes - List of MFA types (optional).
  • authType - The type of authentication ('otp' or other types).

3. verifyAuth

Verifies the user's authentication using OTP or biometrics.

authService.verifyAuth(
  'contact_number_or_email',
  '123456',
  (response) => {
    console.log('Verification successful:', response);
  },
  (error) => {
    console.error('Verification failed:', error);
  },
  false, // isBiometric
  {} // biometricsInput
);

Parameters:

  • contact - The user's contact number or email.
  • password - The OTP or password.
  • onSuccess - Callback function to handle a successful response.
  • onError - Callback function to handle an error response.
  • isBiometric - Whether biometric authentication is used (default is false).
  • biometricsInput - Input for biometric verification (optional).

4. authorizeUser

Authorizes a user with a given token.

authService.authorizeUser(
  'user_token',
  (response) => {
    console.log('Authorization successful:', response);
  },
  (error) => {
    console.error('Authorization failed:', error);
  }
);

Parameters:

  • token - The token to authorize the user.
  • onSuccess - Callback function to handle a successful response.
  • onError - Callback function to handle an error response.

Example

Here’s a full example of using the AuthMiddlewareService in a React component:

import React, { useEffect, useState } from 'react';
import AuthMiddlewareService from 'AuthMiddlewareService';

const authService = new AuthMiddlewareService();

function App() {
  const [message, setMessage] = useState('');
  const [error, setError] = useState('');

  useEffect(() => {
    authService.initAuthMiddleware('your-access-key-here');

    const onSuccess = (response) => {
      setMessage(`Success: ${JSON.stringify(response)}`);
    };

    const onError = (error) => {
      setError(`Error: ${error}`);
    };

    authService.loginWithEmail('test@example.com', 'password123', onSuccess, onError);
  }, []);

  return (
    <div>
      <h1>AuthMiddlewareService Test</h1>
      {message && <p>{message}</p>}
      {error && <p style={{ color: 'red' }}>{error}</p>}
    </div>
  );
}

export default App;

License

MIT License. See the LICENSE file for details.

Readme

Keywords

none

Package Sidebar

Install

npm i @launchlense-ai/authmiddleware-react

Weekly Downloads

2

Version

1.0.0

License

ISC

Unpacked Size

99.4 kB

Total Files

11

Last publish

Collaborators

  • launchlense-ai