A secure authentication API using Node.js, Express, JWT, Google OAuth2, and Two-Factor Authentication (2FA).
✅ User Authentication (Register, Login)
✅ Google OAuth 2.0 Login
✅ JWT Token Authentication
✅ Two-Factor Authentication (2FA) with OTP & QR Code
✅ Secure Routes for logged-in users
✅ Password Hashing with bcrypt
✅ Proper Error Handling & Security Measures
secure-auth-api-nodejs/
│── config/ # Passport & OAuth Configurations
│── models/ # Mongoose User Model
│── routes/ # API Routes (Auth, Users, Protected)
│── middleware/ # Authentication Middleware
│── controllers/ # Business Logic (User handling)
│── .env # Environment Variables
│── server.js # Main Express App
│── package.json # Dependencies & Scripts
│── README.md # Project Documentation
git clone https://github.com/your-username/secure-auth-api-nodejs.git
cd secure-auth-api-nodejs
npm install
Create a .env file in the root directory and add:
PORT=5000
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
EMAIL_SERVICE=email_service_for_2fa
EMAIL_USER=your_email
EMAIL_PASS=your_email_password (App Password)
npm run dev
🚀 Your API will now run on http://localhost:5000
Method | Endpoint | Description |
---|---|---|
POST | /api/auth/register |
Register a new user |
POST | /api/auth/login |
Login and get JWT token |
Method | Endpoint | Description |
---|---|---|
GET | /api/auth/google |
Redirects to Google Login |
GET | /api/auth/google/callback |
Google OAuth callback |
Method | Endpoint | Description |
---|---|---|
GET | /api/profile |
Test Protected Route |
✅ JWT Tokens with expiration
✅ Password Hashing using bcrypt
✅ Two-Factor Authentication (2FA) via email OTP
✅ Session Management for Google OAuth
✅ Error Handling & Input Validation
This project serves as a pre-built secure authentication template for developers seeking a hassle-free and ready-to-use authentication system. It eliminates the need to worry about security concerns, token management, or OAuth integration, allowing developers to focus on building their application rather than spending time on authentication implementation.
This template is designed to be scalable, secure, and easy to integrate, making it the perfect choice for projects requiring a strong authentication system without the complexity.
This project is licensed under the MIT License.
🔗 Live Demo: Coming Soon 🚀
💬 Need Help? Create an issue or reach out! 🎯