Skip to content

[BE-04] Implement JWT token blacklist and refresh token endpoint #262

Description

@Leothosine

Problem

Two authentication gaps exist:

  1. The logout endpoint in src/modules/auth/auth.controller.ts returns a success response but takes no action - the issued JWT remains fully valid until it naturally expires, making logout functionally useless
  2. .env.example defines REFRESH_TOKEN_SECRET and REFRESH_TOKEN_EXPIRES_IN but no refresh token is issued on login and no refresh endpoint exists

Proposed Solution

Token blacklist:

  • On login, include a jti (JWT ID) claim using uuid
  • On logout, store the jti in Redis with TTL equal to the token's remaining lifetime
  • In JwtAuthGuard, check Redis for the jti and reject blacklisted tokens with 401

Refresh token:

  • On login, issue a long-lived refresh token stored as an httpOnly cookie
  • Add POST /api/auth/refresh that validates the refresh token and returns a new access token
  • On logout, also invalidate the refresh token

Acceptance Criteria

  • Using a token after logout returns 401
  • POST /api/auth/refresh with a valid refresh token returns a new access token
  • Expired or tampered refresh tokens return 401
  • Refresh token transmitted via httpOnly cookie (not response body)
  • Integration tests cover: login ? logout ? use old token (401); login ? refresh ? use new token (200)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions