Add JWT authentication guard#884
Merged
Merged
Conversation
…r codes
- Replace src/jwt-auth.guard.ts (was thin AuthGuard('jwt') wrapper) with
full CanActivate implementation:
- Extracts Bearer token from Authorization header
- Verifies signature/expiration via JwtService.verifyAsync
- Checks Redis blacklist: GET blacklist:jti:<jti>
- Attaches decoded payload to req.user
- Structured error responses: missing_token, token_expired,
invalid_token, token_revoked (all 401)
- Optional mode via @OptionalAuth() decorator — passes through
without error for missing/invalid/expired/blacklisted tokens
- Export OptionalAuth decorator for per-route optional auth
- Register JwtAuthGuard + export JwtModule in AuthModule so all
modules using @UseGuards(JwtAuthGuard) get it via DI
Unit tests (10/10):
- valid token → allows + attaches user
- missing Authorization header → 401 missing_token
- expired token → 401 token_expired
- invalid/malformed token → 401 invalid_token
- blacklisted jti → 401 token_revoked
- optional mode: no token, expired, blacklisted → pass through
- optional mode: valid token → attaches user
- performance: <5ms average over 50 iterations
|
@Shecodes174 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #697
Summary
Implements JWT authentication guard for protecting API routes.
This PR ensures that only authenticated users with valid JWT tokens can access protected endpoints.