Skip to main content

Import

Usage

Options

The jwt middleware accepts an options object:
SignatureKey
required
A value of your secret key. Required for token verification.
SignatureAlgorithm
required
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)
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, optional secret for signed cookies, and optional prefixOptions
string
default:"Authorization"
The name of the header to look for the JWT token.
VerifyOptions
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

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 Authorization header 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-Authenticate header with error details on failure
  • Stores decoded payload in context as jwtPayload
  • Requires crypto.subtle.importKey to be available in runtime
  • Supports both header-based and cookie-based token retrieval