Security for NestJSs
NestJS security covers authenticating users, authorising actions and hardening the HTTP surface. Typical pieces are Passport strategies, JWT issuing and verification, guards that check roles, password hashing, plus helmet, CORS and rate limiting through the throttler module.
Stage: Data and Security · Part of the NestJS roadmap · Last reviewed 19 November 2026
What to learn in Security
- Implement login with a Passport local strategy and issue a JWT
- Protect routes with an AuthGuard and a custom roles guard
- Hash passwords with bcrypt or argon2 before storing them
- Enable helmet, CORS rules and rate limiting on the app
- Read the authenticated user in a handler through a custom decorator
Security interview questions
What is a guard in NestJS and when does it run?
A guard is a class implementing CanActivate that decides whether a request may continue. It runs after middleware and before interceptors and pipes, and returns true or false, or throws an exception such as UnauthorizedException.
How do you implement role-based access in NestJS?
Attach roles to a route with a custom decorator using SetMetadata, then write a guard that reads the metadata through Reflector and compares it with the roles on the authenticated user, returning false if none match.
Where Security fits in the NestJS roadmap
Security belongs to the Data and Security stage (intermediate level). Persist and protect.
Other units in this stage
Back to the full NestJS roadmap