8000 GitHub - esmaeil-ahmadipour/task-nest1 · GitHub
[go: up one dir, main page]

Skip to content

About

No description, website, or topics provided.

Resources

Code of conduct

Security policy

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

NestJS JWT Authentication Module with Passport

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.


📸 Postman Testing & Verification Screenshots

All endpoints have been tested and verified via Postman. Below are the actual execution screenshots from the Postman test suite:


1. 🟢 Public Health Check Endpoint (GET /health)

Description: Checks application status and uptime. Bypasses JWT verification using the @Public() custom decorator.

Health Check Endpoint


2. 📝 User Registration (POST /auth/signup)

Description: Creates a new user. The password is encrypted with bcrypt (10 salt rounds) and is completely excluded from the response for security.

User Signup Endpoint


3. 🔑 User Login & Token Generation (POST /auth/login)

Description: Validates credentials against hashed passwords and generates a signed JWT access_token containing { sub, email, role }.

User Login Endpoint


4. 👤 Protected User Profile (GET /auth/profile)

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.

Protected Profile Endpoint


5. 🔄 Token Refreshing Mechanism (POST /auth/refresh)

Description: Seamless token renewal issuing a short-lived access token (15 mins) and a long-lived refresh token (7 days).

Token Refresh Endpoint


6. 🛡️ Admin Account Registration (POST /auth/signup-admin)

Description: Creates a user with elevated admin privileges for testing Role-Based Access Control (RBAC).

Admin Signup Endpoint


7. 👑 Role-Based Access Control (GET /auth/admin)

Description: Route protected by both JwtAuthGuard and RolesGuard requiring @Roles('admin'). Regular users receive a 403 Forbidden error.

Admin Dashboard RBAC Endpoint


🧪 Postman Collection Import

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

🚀 Setup & Execution

1. Installation

$ npm install

2. Run the Application

# Development mode with hot-reload
$ npm run start:dev

# Production mode
$ npm run start:prod

3. Automated Tests

# E2E test suite covering all auth flows & RBAC
$ npm run test:e2e

🌟 Key Architecture & Scalability Highlights

This 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 JwtAuthGuard secures 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 with RolesGuard enabling easy permission management for any number of roles (admin, user, manager, etc.).
  • Standardized Error Handling: Global HttpExceptionFilter transforms all exceptions (including nested class-validator errors) into a uniform API response format: { success: false, statusCode, message }.
  • Strict Input Validation: Powered by class-validator and ValidationPipe with payload whitelist filtering.

📋 API Endpoints Specifications

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

📄 License

This project is UNLICENSED.

About

No description, website, or topics provided.

Resources

Code of conduct

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0