Skip to content

Add JWT authentication guard#884

Merged
nafiuishaaq merged 3 commits into
MentoNest:mainfrom
Shecodes174:feat/jwt-auth-guard
Jun 27, 2026
Merged

Add JWT authentication guard#884
nafiuishaaq merged 3 commits into
MentoNest:mainfrom
Shecodes174:feat/jwt-auth-guard

Conversation

@Shecodes174

@Shecodes174 Shecodes174 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

closes #697

Summary

Implements JWT authentication guard for protecting API routes.

  • Adds JwtAuthGuard for route protection
  • Implements JWT strategy for token validation
  • Configures authentication module with Passport integration
  • Protects critical endpoints with authentication requirements

This PR ensures that only authenticated users with valid JWT tokens can access protected endpoints.

…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
@drips-wave

drips-wave Bot commented Jun 26, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

@nafiuishaaq nafiuishaaq merged commit f705447 into MentoNest:main Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JWT Auth Guard implementation

2 participants