A clean, modern NestJS 11+ REST API demonstrating JWT authentication, TypeScript strict mode, and best practices for scalable Node.js backend.
🛡️ JWT Authentication: Secure login and protected routes using @nestjs/jwt
🔒 Protected Profile: /profile
route requires a valid Bearer token
🧑 In-Memory User Store: No database needed—simple, fast, and easy to test
📄 Swagger Docs: Interactive API docs with JWT support via @nestjs/swagger
🚦 Status Endpoint: /status
public health check
🧪 Unit Tested: Includes Jest test for JWT guard
🧹 Strict TypeScript: Fully typed, idiomatic code
📝 ESLint + Prettier: Consistent code style enforced
🐳 Docker-Ready: Deploy anywhere with Docker & Compose
📬 Postman Collection: Ready-to-use API requests for demo/testing
- Framework: NestJS 11+
- Language: TypeScript (strict mode)
- Auth: JWT via
@nestjs/jwt
- Docs: Swagger (
@nestjs/swagger
) - Testing: Jest
- Linting: ESLint, Prettier
- Containerization: Docker, Docker Compose
- Node.js 18+
- npm or yarn
Clone the repository:
git clone https://github.com/your-username/nestjs-jwt-auth
cd nestjs-jwt-auth
Install dependencies:
npm install
# or
yarn install
Start the development server:
npm run start:dev
Or with Docker:
docker-compose up --build
GET /status
– Public health checkPOST /auth/login
– Login with email/password, returns JWTGET /profile
– Protected, requires Bearer token
- email:
user@example.com
- password:
password
- Visit http://localhost:3000/api for interactive API docs.
- Use the "Authorize" button to test protected endpoints with your JWT.
- See
postman_collection.json
in the repo for ready-to-use requests.
Create a .env
file in the root:
JWT_SECRET=your-secret-key
Build and run with Docker Compose:
docker-compose up --build
Run all tests:
npm test
nestjs-jwt-auth/
├── src/
│ ├── auth/
│ ├── users/
│ ├── app.controller.ts
│ ├── app.module.ts
│ └── main.ts
├── test/
├── .env
├── Dockerfile
├── docker-compose.yml
├── package.json
└── README.md
This project uses a comprehensive .gitignore
to keep your repository clean and secure:
# Node
node_modules/
.env
.env.*
# Logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# OS
.DS_Store
Thumbs.db
# Test coverage
coverage/
# VSCode
.vscode/
# Others
*.local
*.log
# Docker
*.pid
# Jest
coverage/
# Build
build/
# Misc
*.tgz
# Ignore Postman environment files
*.postman_environment.json
- Simplicity: Used in-memory user store for fast prototyping and easy testing.
- Security: JWT authentication with strict validation and guards.
- Developer Experience: Swagger docs, Postman collection, and strict TypeScript for reliability.
- Production-Ready: Dockerized, with environment variable support and clean code style.
- ✅ Unit test for JWT guard (Jest)
- ✅ ESLint + Prettier config
- ✅ Swagger docs with JWT "Authorize" support