A secure, scalable, and production-ready authentication system built with Node.js and Express.
AuthAPI provides a complete authentication workflow including JWT-based authentication, refresh token rotation, OTP email verification, session tracking, Redis-backed rate limiting, request validation, and background job processing using Redis + BullMQ.
- User Registration & Login
- JWT-based Authentication (Access + Refresh Tokens)
- Refresh Token Rotation
- Secure Logout (single session & all sessions)
- OTP-based Email Verification
- Resend OTP support
- Password Reset via OTP
- Login Alert Emails
- Per-device session tracking (IP + User-Agent)
- Refresh tokens stored securely (hashed)
- Session revocation support
- Email queue using BullMQ
- Redis-backed job processing
- Worker runs in same process (can be separated in production)
- Password hashing using bcrypt
- HTTP-only secure cookies
- Token expiration handling
- Centralized error handling middleware
- Input validation using Joi
- Redis-based rate limiting (global + route-specific)
- OTP expiration (10 min)
- Max OTP attempts (5)
- Login alert emails
To prevent abuse, brute-force attacks, and spam, the API uses Redis-backed rate limiting with fine-grained control per route.
- Applied to all routes
- 100 requests per minute per IP
- Applied to sensitive auth routes (register, refresh, forgot password, etc.)
- 20 requests per 15 minutes per IP + email
- Applied only to login route
- 5 attempts per 15 minutes per IP + email
- Protects against brute-force attacks
- Applied to OTP-related routes
- 10 requests per 10 minutes per IP + email + endpoint
- Prevents OTP spamming
- All rate limits are stored in Redis
- Ensures scalability across multiple instances
- Node.js
- Express.js
- MongoDB + Mongoose
- JWT (jsonwebtoken)
- bcryptjs
- Redis
- BullMQ
- Nodemailer (OAuth2)
- Joi (Validation)
AuthAPI/
βββ src/
β βββ config/ # DB & environment configs
β βββ controllers/ # Business logic (auth flow)
β βββ middleware/ # Auth + validation + rate limiting
β βββ models/ # Mongoose schemas
β βββ queues/ # BullMQ queues & workers
β βββ routes/ # API routes
β βββ services/ # Email service
β βββ utils/ # Helpers (OTP, asyncHandler)
β βββ validators/ # Joi schemas
β βββ app.js # Express app
β
βββ server.js # Entry point
βββ .env
βββ package.json
βββ README.mdCreate a .env file:
PORT=5000
MONGO_URL=your_mongodb_connection
JWT_SECRET=your_secret_key
# Email (Gmail OAuth2)
EMAIL_USER=your_email
CLIENT_ID=your_client_id
CLIENT_SECRET=your_client_secret
REFRESH_TOKEN=your_refresh_token
# Redis
REDIS_URL=your_redis_url
# Clone repository
git clone https://github.com/Ishu6129/AuthAPI.git
# Move into project
cd AuthAPI
# Install dependencies
npm install
# Run dev server
npm run dev| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/register |
Register user |
| POST | /api/auth/login |
Login user |
| GET | /api/auth/get-me |
Get current user (protected) |
| POST | /api/auth/refresh |
Refresh access token |
| POST | /api/auth/logout |
Logout current session |
| POST | /api/auth/logout-all |
Logout all sessions |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/verify-email |
Verify email using OTP |
| POST | /api/auth/new-otp |
Request new OTP |
| POST | /api/auth/forgot-password |
Send reset OTP |
| POST | /api/auth/reset-password |
Reset password using OTP |
-
User registers β OTP sent via email queue
-
User verifies email
-
User logs in β receives:
- Access Token (15 min)
- Refresh Token (7 days, stored in cookie)
-
Session created with:
- IP address
- User-Agent
-
Refresh token rotates on every refresh request
-
Logout revokes session(s)
Centralized error handling via middleware:
- MongoDB duplicate key β
409 Conflict - Validation errors β
400 Bad Request - JWT errors β
401 Unauthorized - Default β
500 Internal Server Error
Handled automatically using asyncHandler.
All incoming requests are validated using Joi schemas via a reusable middleware:
validate("register")
validate("login")
validate("email")
validate("resetPassword")- Prevents invalid data from reaching controllers
- Returns all validation errors (
abortEarly: false) - Removes unwanted fields (
stripUnknown: true) - Ensures clean and secure request payloads
- Email sending is offloaded to Redis queue
- Worker processes jobs asynchronously
- Improves performance & scalability
Worker currently runs in the same process (can be separated in production).
- Wraps async controllers
- Automatically forwards errors to global handler
- Global middleware
- Handles all errors (Mongo, JWT, validation, etc.)
- Middleware for request validation
- Uses Joi schemas
- Cleans and validates request body
- Tracks device-based login
- Stores hashed refresh tokens
- Enables secure logout & session control
npm run dev # Development (nodemon)MIT License
Ishu Agrawal GitHub: https://github.com/Ishu6129