A production-ready, scalable, and modular authentication system built with NestJS, Passport, and JWT. Featuring robust security patterns, Role-Based Access Control (RBAC), and clean architecture.
All endpoints have been tested and verified via Postman. Below are the actual execution screenshots from the Postman test suite:
Description: Checks application status and uptime. Bypasses JWT verification using the
@Public()custom decorator.
Description: Creates a new user. The password is encrypted with bcrypt (10 salt rounds) and is completely excluded from the response for security.
Description: Validates credentials against hashed passwords and generates a signed JWT
access_tokencontaining{ sub, email, role }.
Description: Accesses a protected route using the
Authorization: Bearer <token>header. The user data is extracted directly from the decoded token payload without database overhead.
Description: Seamless token renewal issuing a short-lived access token (15 mins) and a long-lived refresh token (7 days).
Description: Creates a user with elevated
adminprivileges for testing Role-Based Access Control (RBAC).
Description: Route protected by both
JwtAuthGuardandRolesGuardrequiring@Roles('admin'). Regular users receive a403 Forbiddenerror.
A complete Postman Collection is included in the root directory:
📁 postman_collection.json
- Auto-Token Script: Automatically extracts and attaches tokens to subsequent requests.
- Pre-configured Tests: Covers public, protected, role-based, and error scenarios (400, 401, 403).
$ npm install# Development mode with hot-reload
$ npm run start:dev
# Production mode
$ npm run start:prod# E2E test suite covering all auth flows & RBAC
$ npm run test:e2eThis module is designed adhering to Clean Architecture and enterprise design principles:
- Clean & Modular Structure: Clear separation of concerns between Controllers (HTTP layer), Services (Business Logic), Strategies (Auth verification), and DTOs (Data validation).
- High Scalability:
- Stateless JWT token flow ensures horizontal scaling across multiple server instances without session state synchronization.
- In-memory data store is abstracted behind service methods, making database integration (Prisma, TypeORM, Mongoose) seamless without touching controllers or guards.
- Global Security by Default: Global
JwtAuthGuardsecures all endpoints automatically. Public endpoints are explicitly marked via custom@Public()metadata decorator, preventing accidental data leaks. - Granular Role-Based Access Control (RBAC): Extensible
@Roles()decorator withRolesGuardenabling easy permission management for any number of roles (admin,user,manager, etc.). - Standardized Error Handling: Global
HttpExceptionFiltertransforms all exceptions (including nested class-validator errors) into a uniform API response format:{ success: false, statusCode, message }. - Strict Input Validation: Powered by
class-validatorandValidationPipewith payload whitelist filtering.
| Method | Endpoint | Access Level | Description | Status Code |
|---|---|---|---|---|
GET |
/health |
🟢 Public (@Public()) |
Server health status & uptime | 200 OK |
POST |
/auth/signup |
🟢 Public (@Public()) |
Registers a new user with bcrypt password hashing | 201 Created |
POST |
/auth/login |
🟢 Public (@Public()) |
Validates credentials & returns JWT access token | 201 Created |
GET |
/auth/profile |
🔒 Protected (Bearer JWT) | Returns authenticated user data from token payload | 200 OK |
POST |
/auth/refresh |
🔒 Protected (Bearer JWT) | Issues fresh access (15m) & refresh (7d) tokens | 201 Created |
POST |
/auth/signup-admin |
🟢 Public (@Public()) |
Registers an administrator account | 201 Created |
GET |
/auth/admin |
👑 Admin Only (@Roles('admin')) |
Protected dashboard for admin role | 200 OK |
This project is UNLICENSED.