Overview
This page contains 7 code snippets for implementing Authentication in NestJS. Each snippet is production-ready and can be copy-pasted directly into your project.
Available Snippets
Current User
Extract authenticated user from request
- Extract full user object
- Extract specific property
- Type-safe user data
Roles Decorator
Role-based access control decorator
- Role enum included
- Single role requirement
- Multiple roles (OR logic)
Public Route
Mark routes as public to skip auth
- Skip auth guards
- Works with global guards
- Simple metadata decorator
Auth Decorator
Combined authentication decorator
- JWT + Roles combined
- Swagger integration
- AdminOnly shortcut
JWT Auth Guard
JWT authentication with Passport
- Passport integration
- Public route support
- Token expiration handling
API Key Guard
API key authentication for services
- Timing-safe comparison
- Multiple extraction methods
- Multiple API keys support
Password Utilities
Argon2 password hashing
- Argon2id hashing
- OWASP configuration
- Strength checking
Frequently Asked Questions
How do I implement Authentication in NestJS?
Browse the snippets above and copy the code that matches your needs. Each snippet includes installation instructions and usage examples.
Are these snippets production-ready?
Yes, all snippets are designed for production use. However, always review and test them in your specific context before deployment.
Can I use these snippets with other NestJS versions?
These snippets are designed for the current major version of the framework. Check the snippet documentation for version compatibility notes.