Skip to content

Ishu6129/AuthAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” AuthAPI

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.

πŸš€ Features

πŸ”‘ Authentication

  • User Registration & Login
  • JWT-based Authentication (Access + Refresh Tokens)
  • Refresh Token Rotation
  • Secure Logout (single session & all sessions)

πŸ“§ Email & OTP

  • OTP-based Email Verification
  • Resend OTP support
  • Password Reset via OTP
  • Login Alert Emails

🧠 Session Management

  • Per-device session tracking (IP + User-Agent)
  • Refresh tokens stored securely (hashed)
  • Session revocation support

βš™οΈ Background Processing

  • Email queue using BullMQ
  • Redis-backed job processing
  • Worker runs in same process (can be separated in production)

πŸ›‘οΈ Security

  • 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

🚦 Rate Limiting

To prevent abuse, brute-force attacks, and spam, the API uses Redis-backed rate limiting with fine-grained control per route.

πŸ”Ή Global Limiter

  • Applied to all routes
  • 100 requests per minute per IP

πŸ”Ή Auth Limiter

  • Applied to sensitive auth routes (register, refresh, forgot password, etc.)
  • 20 requests per 15 minutes per IP + email

πŸ”Ή Login Limiter (Strict)

  • Applied only to login route
  • 5 attempts per 15 minutes per IP + email
  • Protects against brute-force attacks

πŸ”Ή OTP Limiter

  • Applied to OTP-related routes
  • 10 requests per 10 minutes per IP + email + endpoint
  • Prevents OTP spamming

πŸ”Ή Storage

  • All rate limits are stored in Redis
  • Ensures scalability across multiple instances

πŸ› οΈ Tech Stack

  • Node.js
  • Express.js
  • MongoDB + Mongoose
  • JWT (jsonwebtoken)
  • bcryptjs
  • Redis
  • BullMQ
  • Nodemailer (OAuth2)
  • Joi (Validation)

πŸ“ Project Structure

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.md

βš™οΈ Environment Variables

Create 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

▢️ Installation & Setup

# 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

πŸ”— API Endpoints

πŸ§‘β€πŸ’» Authentication

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

πŸ“§ Email & OTP

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

πŸ” Authentication Flow

  1. User registers β†’ OTP sent via email queue

  2. User verifies email

  3. User logs in β†’ receives:

    • Access Token (15 min)
    • Refresh Token (7 days, stored in cookie)
  4. Session created with:

    • IP address
    • User-Agent
  5. Refresh token rotates on every refresh request

  6. Logout revokes session(s)

⚠️ Error Handling

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.

🧾 Request Validation

All incoming requests are validated using Joi schemas via a reusable middleware:

validate("register")
validate("login")
validate("email")
validate("resetPassword")

Features:

  • Prevents invalid data from reaching controllers
  • Returns all validation errors (abortEarly: false)
  • Removes unwanted fields (stripUnknown: true)
  • Ensures clean and secure request payloads

⚑ Background Jobs (BullMQ)

  • 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).

🧠 Key Concepts

βœ… asyncHandler

  • Wraps async controllers
  • Automatically forwards errors to global handler

βœ… errorHandler

  • Global middleware
  • Handles all errors (Mongo, JWT, validation, etc.)

βœ… validate

  • Middleware for request validation
  • Uses Joi schemas
  • Cleans and validates request body

βœ… Session Model

  • Tracks device-based login
  • Stores hashed refresh tokens
  • Enables secure logout & session control

πŸ§ͺ Scripts

npm run dev     # Development (nodemon)

πŸ“„ License

MIT License

πŸ’‘ Author

Ishu Agrawal GitHub: https://github.com/Ishu6129

About

Production-ready authentication system with JWT, refresh token rotation, OTP verification, and Redis-powered background jobs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors