Verification Results

Last updated


This page explains what the three flags of the VerifyResult returned by VerifyJWT() mean, and which values each scenario produces.

Shape


  1. interface VerifyResult {
  2. data?: AuthData;
  3. isAuth: boolean;
  4. isError: boolean;
  5. isGuest: boolean;
  6. }

FlagMeaning
isAuthVerification succeeded; data holds the user
isErrorThe request looks suspicious (fingerprint mismatch, bad signature); respond with 400
isGuestNot logged in; always true when isAuth is false

Scenario Table


ScenarioisAuthisErrorisGuest
Valid Access Token, fingerprint matchestruefalsefalse
Access Token expired, refresh succeededtruefalsefalse
No Access Token and no Refresh IDfalsefalsetrue
Access Token on the revocation blacklistfalsefalsetrue
checkUserExists() returned falsefalsefalsetrue
Refresh data fingerprint mismatchfalsefalsetrue
Access Token fingerprint mismatchfalsetruetrue
Invalid signature or malformed tokenfalsetruetrue
Refresh data missingfalsetruetrue

Routing on the Result


  1. import { NextFunction, Request, Response } from "express";
  2. import { JWTAuth } from "@pardnchiu/jwt-auth";
  3. export async function requireAuth(req: Request, res: Response, next: NextFunction) {
  4. try {
  5. const result = await JWTAuth.VerifyJWT(req, res);
  6. if (result.isAuth) {
  7. res.locals.user = result.data;
  8. return next();
  9. }
  10. res.status(result.isError ? 400 : 401).json({
  11. error: result.isError ? "Bad Request" : "Unauthorized",
  12. });
  13. } catch (err) {
  14. // Reached when init() was never called or the blacklist lookup hits a Redis error
  15. res.status(500).json({ error: (err as Error).message });
  16. }
  17. }

Legacy Return Values


Older VerifyJWT() versions returned AuthData or the status codes 400 / 401. JWTAuth.GetAuth() converts those into the same three-flag shape:
InputOutput
AuthData{ ...data, isAuth: true, isError: false, isGuest: false }
401{ isAuth: false, isError: false, isGuest: true }
400{ isAuth: false, isError: true, isGuest: true }
Any other number{ isAuth: true, isError: false, isGuest: false }

Related: Refresh Token Rotation, Device Fingerprint
中文