Import
Usage
Options
ThebearerAuth middleware accepts a BearerAuthOptions object:
The token or array of tokens to validate against. Required if
verifyToken is not provided.Custom function to verify the token. Alternative to providing static
token values.The domain name of the realm, as part of the returned WWW-Authenticate challenge header.
The prefix (or schema) for the Authorization header value. If set to empty string, no prefix is expected.
The header name to look for the token.
A function to handle hashing for safe comparison of authentication tokens.
The message returned when no authentication header is provided.
noAuthenticationHeader.wwwAuthenticateHeader
string | object | MessageFunction
default:"Bearer realm=\"\""
The WWW-Authenticate header value when no authentication header is provided.
The message returned when authentication header format is invalid.
invalidAuthenticationHeader.wwwAuthenticateHeader
string | object | MessageFunction
default:"Bearer error=\"invalid_request\""
The WWW-Authenticate header value when authentication header is invalid.
The message returned when token is invalid.
invalidToken.wwwAuthenticateHeader
string | object | MessageFunction
default:"Bearer error=\"invalid_token\""
The WWW-Authenticate header value when token is invalid.
Signature
Examples
Multiple tokens
Custom token verification
Custom prefix
No prefix
Custom error messages
Behavior
- Returns 401 Unauthorized if token is missing or invalid
- Returns 400 Bad Request if Authorization header format is invalid
- Sets WWW-Authenticate header with appropriate error information
- Uses timing-safe comparison to prevent timing attacks
- Supports custom hash functions for secure token comparison