1.0.3 • Published 8 months ago

2fauth v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

2FAuth - Secure Authentication with JWT & 2FA

npm license downloads

šŸš€ 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

MethodEndpointDescription
POST/auth/registerRegister a new user
GET/auth/verify-emailVerify user email
GET/auth/resend-verificationVerify user email
POST/auth/loginUser Login
POST/auth/enable-2faEnable 2FA (Generates QR code)
POST/auth/disable-2faDisable 2FA
POST/auth/verify-2faVerify OTP for 2FA
POST/auth/logoutLogout the user
POST/auth/forgot-passwordInitiate password reset
POST/auth/reset-passwordReset password
PUT/auth/change-passwordChange the current password
GET/auth/current-userFetch Current User
PUT/auth/update-user/:userIdUpdate 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!