Overview
The JWT Strategy handles token validation and user authentication for all protected endpoints in Walle. It’s implemented using Passport.js and extracts user information from JWT tokens. Source:src/app/auth/strategies/jwt.strategy.ts
JwtStrategy Class
TheJwtStrategy class extends PassportStrategy(Strategy) from @nestjs/passport and is responsible for:
- Extracting JWT tokens from request headers
- Validating token signatures
- Looking up authenticated users from MongoDB
- Providing user context to protected routes
Implementation
src/app/auth/strategies/jwt.strategy.ts:10-28
Constructor Configuration
The strategy is configured in the constructor atsrc/app/auth/strategies/jwt.strategy.ts:11-20:
Token Extraction
Method:
ExtractJwt.fromAuthHeaderAsBearerToken()Extracts JWT from the Authorization header using Bearer scheme:Expiration Handling
When
false, expired tokens are automatically rejected. Tokens in Walle expire after 8 hours.Secret Key
Retrieved from
ConfigService using the JWT_SECRET constant. This secret is used to verify the token’s signature.Source: config.get<string>(JWT_SECRET)validate() Method
Thevalidate() method is called automatically by Passport after the JWT signature is verified.
Location: src/app/auth/strategies/jwt.strategy.ts:22-27
Parameters
The decoded JWT payload containing user information. Expected to include:
user_dni(number): User’s DNI used for database lookup- Additional claims as needed
Return Value
Returns the full user document from MongoDB if found, or
null if:- The payload doesn’t contain
user_dni - The user is not found in the database
null is returned, the request is rejected with a 401 Unauthorized error.Validation Flow
User Lookup
The strategy usesUserService.findOneDni() to retrieve users:
Usage in Guards
The JWT Strategy is used byJwtAuthguard to protect endpoints:
- Invokes the JWT strategy
- Checks if
validate()returned a user - Throws
UnauthorizedExceptionif validation failed - Attaches the user to the request object if successful
Module Registration
The strategy is registered in the AuthModule:src/app/auth/auth.module.ts:9-24
Configuration Requirements
Environment Variables
Secret key for signing and verifying JWT tokens. Must be configured in your environment:
Database Connection
MongoDB database name where users are stored. Referenced in
UserService at src/app/user/user.service.ts:10.User Document Schema
Thevalidate() method returns a UserDocument with the following key fields:
Unique DNI identifier used for authentication
Username
Email address
User’s role in the system
Whether the user account is active
Root/superuser flag
Administrator flag
src/app/user/schema/user.schema.ts:11-250
Error Handling
Invalid Token
When the JWT signature is invalid or the token is malformed:User Not Found
Whenvalidate() returns null (user_dni not in payload or user not in database):
Expired Token
When the token is older than 8 hours:Best Practices
- Token Payload: Always include
user_dniin JWT payload when signing tokens - User Validation: The strategy performs a fresh database lookup on every request for security
- Null Handling: Ensure user accounts exist before issuing tokens
- Secret Rotation: Rotate
JWT_SECRETperiodically for enhanced security - Error Messages: Generic “Unathorized!” message prevents information leakage