bearerAuth when your clients authenticate with a pre-shared API key or token. Use basicAuth for human-facing admin interfaces or simple service-to-service authentication. Use jwtAuth when tokens are issued by an identity provider and carry claims you need downstream — it verifies signatures entirely via Web Crypto (crypto.subtle), with no external dependencies.
Bearer token auth
bearerAuth extracts the Authorization: Bearer <token> header and validates it. It supports three validation strategies: a static token string, a dynamic secret resolver (useful when your token lives in KV or an environment binding), and a fully custom async validator.
All token comparisons use a constant-time equality check to prevent timing attacks.
BearerAuthOptions
bearerAuth responds with 401 Unauthorized and sets the WWW-Authenticate: Bearer realm="Blaze" response header automatically.
HTTP Basic Auth
basicAuth decodes the Authorization: Basic <base64> header, splits the colon-delimited credentials, and compares them against the expected username and password using a constant-time comparison.
BasicAuthOptions
basicAuth responds with 401 and sets WWW-Authenticate: Basic realm="Admin Panel", causing browsers to show their native credential prompt.
JWT auth
jwtAuth verifies JSON Web Tokens using the Web Crypto API — no jsonwebtoken or jose packages required. It supports HS256 (HMAC-SHA256 shared secret) and RS256 (RSA-SHA256 public key), and validates exp, nbf, iss, and aud claims automatically. After successful verification, it sets the decoded payload on req.user.
JwtAuthOptions
jwtAuth middleware accepts the public key in either PEM (SPKI) format (a string beginning with -----BEGIN PUBLIC KEY-----) or as a JSON Web Key string (a stringified JWK object). For RS256, store the public key as a Cloudflare Secret — even though it’s public, keeping it in req.env makes rotation easy without redeploying.
If the token is missing, expired, signed with the wrong algorithm, or fails signature verification, jwtAuth responds with 401 and a descriptive error message.
Protecting routes
1
Global API protection
Apply auth to every route under a path prefix using
app.use():2
Per-route auth
Inline middleware on individual routes for mixed public/private APIs:
3
Sub-router with shared auth
Use a Router to apply one auth strategy to a group of routes without touching global middleware:
TypeScript: typed req.user
jwtAuth sets the decoded payload on req.user at runtime, but TypeScript doesn’t know about it by default. Augment the BlazeRequest interface in a declaration file to make req.user fully typed throughout your project.
req.user in your route handlers — no casting required.