API Reference

Last updated


This page lists the signature, behavior, and errors of every public JWTAuth method.

Import


  1. import { JWTAuth } from "@pardnchiu/jwt-auth";
  2. // or the default import
  3. import JWTAuth from "@pardnchiu/jwt-auth";

JWTAuth has only static methods; its constructor is private and cannot be called with new.

Methods


MethodSignature
initinit(config: Config): Promise<void>
closeclose(): Promise<void>
CreateJWTCreateJWT(req: Request, res: Response, user: AuthData): Promise<TokenResult>
VerifyJWTVerifyJWT(req: Request, res: Response): Promise<VerifyResult>
RevokeJWTRevokeJWT(req: Request, res: Response, refresh_id?: string): Promise<void>
GetAuthGetAuth(auth: AuthData | number)

init


Loads keys, applies cookie name defaults, and connects to Redis. See Configuration for errors.

close


Disconnects from Redis and clears the connection reference. Logs and rethrows when disconnecting fails.

CreateJWT


StepBehavior
1Computes the fingerprint and derives a Refresh ID
2Signs an ES256 Access Token with id, name, email, thumbnail, level, role, scope (default []), fp, and refresh_id
3Writes refresh:<id> (version: 1) with TTL refreshTokenExpires
4Sets the access and refresh cookies

Returns { token, refresh_id }. Throws JWTAuth not initialized. Call JWTAuth.init() first. when not initialized; a Redis write error propagates after the access cookie has already been set.

VerifyJWT


Verifies the Access Token; on expiry it refreshes and writes back cookies and X-New-* headers. It throws when not initialized, or when the first Redis lookup (the revoke: blacklist check) fails; every other failure is a VerifyResult (see Verification Results). Refresh details are in Refresh Token Rotation.

RevokeJWT


Clears cookies, shortens the refresh data TTL, and blacklists the Access Token. The refresh_id argument takes precedence over the request. Redis errors are logged, not thrown. See Token Revocation for cases where nothing is revoked.

GetAuth


Converts a legacy VerifyJWT() return value (AuthData or 400 / 401) into { ...data, isAuth, isError, isGuest }. Any number other than 400 / 401 yields isAuth: true.
Related: Types
中文