A NestJS authentication implementation using Passport.js with Local and JWT authentication strategies. This project demonstrates how to implement secure authentication in a NestJS application with protected routes, guards, and token-based authentication.
- 🔐 Local Authentication Strategy - Username/password based authentication
- 🎫 JWT Authentication Strategy - Token-based authentication for protected routes
- 🛡️ Authentication Guards - Protect routes with
LocalAuthGuardandJwtAuthGuard - 👤 User Management - User service for managing user data
- 🔒 Secure Token Generation - JWT tokens for stateless authentication
- Login: Users authenticate with username/password using the local strategy
- Token Generation: Upon successful authentication, a JWT access token is generated
- Protected Routes: Use the JWT token in the Authorization header to access protected endpoints
src/
├── auth/ # Authentication module
│ ├── auth.module.ts
│ ├── auth.service.ts
│ ├── local.strategy.ts # Local (username/password) strategy
│ ├── jwt.strategy.ts # JWT token validation strategy
│ ├── local-auth.guard.ts # Guard for local authentication
│ ├── jwt-auth.guard.ts # Guard for JWT protected routes
│ └── constants.ts # JWT secret (excluded from git)
├── users/ # User management module
│ ├── users.module.ts
│ └── users.service.ts
└── httpEndpoints/ # HTTP request examples
├── login.http
└── profile.http
- The
constants.tsfile containing JWT secrets is excluded from version control (see.gitignore) - Make sure to set a strong JWT secret in production
- This is a starter template - enhance user validation and password hashing for production use
POST /auth/login
- Authenticate with username and password
- Returns a JWT access token
- Request body:
{ "username": "john", "password": "changeme" } - Response:
{ "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." }
GET /profile
- Get user profile (requires authentication)
- Requires JWT token in Authorization header
- Header:
Authorization: Bearer <your-jwt-token> - Returns the authenticated user object
$ npm install# watch mode
$ npm run start:dev