Skip to content

[Backend] optionalAuth middleware skips revocation check — revoked tokens still authenticate on public routes #854

Description

@OlaGreat

Summary

requireAuth correctly queries prisma.revokedToken.findUnique() before accepting a token. optionalAuth does not — it accepts any structurally valid JWT including revoked ones.

This means: after a user logs out (and their jti is added to RevokedToken), routes protected by optionalAuth will still populate req.auth with the revoked token's claims, allowing the user to appear authenticated on those routes.

Location

backend/src/auth.ts, optionalAuth function (line ~118)

Fix

Apply the same revocation check inside optionalAuth after calling verifyJWT:

if (auth.jti) {
  const revoked = await prisma.revokedToken.findUnique({ where: { jti: auth.jti } });
  if (revoked) return next(); // treat as unauthenticated
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendExpress / Prisma / API workbugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions