1.0.3 ⢠Published 8 months ago
2fauth v1.0.3
2FAuth - Secure Authentication with JWT & 2FA
š 2FAuth is a plug-and-play authentication system for Node.js/Express apps, featuring:
ā
User Registration & Login
ā
JWT Authentication
ā
Email Verification
ā
Two-Factor Authentication (2FA)
ā
Password Reset
šļø Installation
Install 2FAuth via NPM:
npm install 2fauth
š Usage
Import and initialize authentication in your Express app:
const express = require("express");
const { initAuth } = require("2fauth");
const app = express();
app.use(
initAuth({
mongoUrl: "mongodb+srv://your-mongo-db",
jwtSecretKey: "your-secret-key",
emailConfig: {
fromEmail: "noreply@example.com",
transportOptions: {
service: "gmail",
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASSWORD,
},
},
},
})
);
app.listen(5000, () => console.log("Auth system running!"));
š” API Endpoints
Method | Endpoint | Description |
---|---|---|
POST | /auth/register | Register a new user |
GET | /auth/verify-email | Verify user email |
GET | /auth/resend-verification | Verify user email |
POST | /auth/login | User Login |
POST | /auth/enable-2fa | Enable 2FA (Generates QR code) |
POST | /auth/disable-2fa | Disable 2FA |
POST | /auth/verify-2fa | Verify OTP for 2FA |
POST | /auth/logout | Logout the user |
POST | /auth/forgot-password | Initiate password reset |
POST | /auth/reset-password | Reset password |
PUT | /auth/change-password | Change the current password |
GET | /auth/current-user | Fetch Current User |
PUT | /auth/update-user/:userId | Update the current user |
š Usage Examples
š¹ Register a New User
curl -X POST http://localhost:8000/auth/register \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "johndoe@example.com",
"password": "securepassword"
}'
š Response (Success)
{
"success": true,
"message": "User registered successfully. Please verify your email"
}
š¹ Login a User
curl -X POST http://localhost:8000/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "johndoe@example.com",
"password": "securepassword"
}'
š Response (Success)
{
"success": true,
"message": "Login Successful"
}
š¤ Contributing
Contributions are welcome! If you find a bug or want to add a feature, feel free to open an issue or submit a PR.
āļø License
MIT License - Free to use and modify!