Authentication Infrastructure
Walle provides JWT (JSON Web Token) authentication infrastructure using Passport.js. The authentication system includes strategy implementation, guards, and decorators ready to be used in your application.The authentication infrastructure is implemented, but you’ll need to create your own auth controller with login/signup endpoints. The core components (JwtStrategy, JwtAuthguard, @Auth decorator) are ready to use.
Core Components
JwtStrategy
TheJwtStrategy class validates JWT tokens and retrieves user data from MongoDB:
- Extracts JWT from Authorization header (Bearer token)
- Validates token signature using
JWT_SECRET - Rejects expired tokens
- Looks up user by
user_dnifrom token payload - Returns user document or null
JwtAuthguard
TheJwtAuthguard extends Passport’s AuthGuard and throws exceptions for unauthorized requests:
- Returns 401 Unauthorized if token is missing, invalid, or expired
- Returns 401 if user lookup fails
- Populates request.user with the User document on success
@Auth() Decorator
The@Auth() decorator provides a convenient way to protect endpoints:
JWT Configuration
JWT tokens are configured insrc/app/auth/auth.module.ts:
- Secret: Loaded from
JWT_SECRETenvironment variable - Expiration: 8 hours
- Algorithm: HS256 (default)
Required Environment Variable
Secret key used to sign and verify JWT tokens. Use a strong random string in production.
Implementing Login Endpoint
To use the authentication infrastructure, you’ll need to create a login controller:Token Usage
Include JWT tokens in requests using the Authorization header:Error Handling
401 Unauthorized
TheJwtAuthguard throws UnauthorizedException when:
- Authorization header is missing
- Token is malformed or invalid signature
- Token has expired (>8 hours)
- User lookup by
user_dnireturns null
User Validation
The JWT payload must containuser_dni field. The JwtStrategy.validate() method:
- Checks if payload contains
user_dni - Calls
UserService.findOneDni(payload.user_dni) - Returns User document if found, null otherwise
- JwtAuthguard throws 401 if user is null
src/app/user/user.service.ts:
Security Best Practices
Token Storage
Token Storage
Store JWT tokens securely:
- Use httpOnly cookies for web applications
- Use secure storage APIs for mobile apps
- Never store tokens in localStorage if you handle sensitive data
Secret Key Security
Secret Key Security
Protect your JWT_SECRET:
- Use a strong random string (minimum 32 characters)
- Never commit secrets to version control
- Rotate secrets periodically
- Use different secrets for dev/staging/production
HTTPS Required
HTTPS Required
Always use HTTPS in production:
- JWT tokens in Authorization headers are sent with every request
- HTTPS prevents token interception
- Configure CORS properly for browser-based clients
Token Expiration
Token Expiration
The 8-hour expiration requires:
- Implement token refresh logic for long-lived sessions
- Handle 401 errors gracefully in your client
- Prompt users to re-authenticate when tokens expire
Next Steps
JWT Strategy
Deep dive into JwtStrategy implementation
User Service
Learn about user lookup and management
User Schema
Explore the complete User model
Environment Config
Configure JWT_SECRET and other variables