Import
Usage
Options
Thejwt middleware accepts an options object:
A value of your secret key. Required for token verification.
An algorithm type that is used for verifying. Available types are:
HS256,HS384,HS512(HMAC)RS256,RS384,RS512(RSA)PS256,PS384,PS512(RSA-PSS)ES256,ES384,ES512(ECDSA)EdDSA(EdDSA)
cookie
string | { key: string; secret?: string | BufferSource; prefixOptions?: CookiePrefixOptions }
If this value is set, then the JWT is retrieved from the cookie using the specified key. Can be:
- A string: the cookie name
- An object with
key, optionalsecretfor signed cookies, and optionalprefixOptions
The name of the header to look for the JWT token.
Additional options for JWT payload verification (e.g., audience, issuer, expiration checks).
Signature
Context Variable
The middleware sets the decoded JWT payload in the context:Examples
Basic usage with secret
Custom header name
JWT from cookie
Signed cookie
With verification options
Using RSA keys
Utility Functions
sign
Sign a JWT token.verify
Verify a JWT token.decode
Decode a JWT token without verification.verifyWithJwks
Verify a JWT token using JWKS.Behavior
- Expects JWT in
Authorizationheader with format:Bearer <token> - Returns 401 Unauthorized if:
- No authorization header or cookie is present
- Token format is invalid (not exactly 2 parts)
- Token verification fails
- Sets
WWW-Authenticateheader with error details on failure - Stores decoded payload in context as
jwtPayload - Requires
crypto.subtle.importKeyto be available in runtime - Supports both header-based and cookie-based token retrieval