AuthMiddlewareService
is an npm package for managing authentication via various API endpoints. It provides methods for login, OTP initialization, verification, and authorization.
To use AuthMiddlewareService
in your React project, install it via npm:
npm install @launchlense-ai/authmiddleware-react
- Login/Signup to https://portal.amw.launchlense.tech.
- Create a new Project.
- You will get a access token as soon as you create a project. (!!!Do not loose the access token!!!)
First, import the AuthMiddlewareService
class into your React component:
import AuthMiddlewareService from 'AuthMiddlewareService';
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.
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.
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 isfalse
). -
mfaTypes
- List of MFA types (optional). -
authType
- The type of authentication ('otp' or other types).
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 isfalse
). -
biometricsInput
- Input for biometric verification (optional).
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.
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;
MIT License. See the LICENSE file for details.