An easy-to-use module for handling user and admin authentication via email OTP. This module supports login and signup flows for both users and admins, providing a robust and secure way to manage authentication in your application.
-
Login as User:
- Users can log in using their email and a One-Time Password (OTP) sent to their email.
-
Login as Admin:
- Admins can log in using their email and a One-Time Password (OTP) sent to their email.
-
Signup as User:
- Users can sign up by entering their email and receiving an OTP to verify their account.
-
Signup as Admin:
- Admins can sign up by entering their email. An OTP will be sent to the super admin's email for verification before the account is created.
- Node.js: Ensure you have Node.js installed (>= 14.x recommended).
- npm: Ensure npm is available for managing dependencies.
Install the complete-auth
package via npm:
npm install complete-auth
In the root directory of your project, create a .env
file to configure the backend API endpoint:
VITE_BACKEND_ENDPOINT=http://localhost:8000/api
- Replace
http://localhost:8000/api
with your actual backend API URL.
Import the module into your application and initialize it.
import { login, signup } from "authentication-module";
login("user", {
email: "user@example.com",
otp: "123456", // OTP sent to the user's email
})
.then((response) => console.log("Login successful:", response))
.catch((error) => console.error("Login failed:", error));
login("admin", {
email: "admin@example.com",
otp: "123456", // OTP sent to the admin's email
})
.then((response) => console.log("Admin login successful:", response))
.catch((error) => console.error("Admin login failed:", error));
signup("user", {
email: "user@example.com",
})
.then((response) => console.log("Signup successful:", response))
.catch((error) => console.error("Signup failed:", error));
signup("admin", {
email: "admin@example.com",
})
.then((response) => console.log("Admin signup request sent:", response))
.catch((error) => console.error("Admin signup failed:", error));
Ensure the backend API supports the following endpoints:
-
POST
/user/login
:- Handles user and admin login with email OTP.
-
POST
/user/signup
:- Handles user signup and sends OTP to the user's email.
-
POST
user/signup/admin
:- Sends an OTP to the super admin for admin account creation.
Variable | Description | Example |
---|---|---|
VITE_BACKEND_ENDPOINT |
Backend API base URL | http://localhost:8000/api |
-
Easy Integration:
- Simplifies OTP-based login and signup processes for users and admins.
-
Secure:
- Ensures account verification through OTP mechanisms.
-
Customizable:
- Supports environment-based configuration for different backend endpoints.
-
"Module not found" Error:
- Ensure the package is installed correctly using
npm install complete-auth
.
- Ensure the package is installed correctly using
-
Backend Not Responding:
- Verify the
VITE_BACKEND_ENDPOINT
in the.env
file is pointing to a valid backend API.
- Verify the
-
Login/Signup Fails:
- Ensure the backend API is running and properly configured to handle the respective endpoints.
Happy authenticating! 🎉