Import
Usage
Options
ThebearerAuth middleware accepts a BearerAuthOptions object:
string | string[]
The token or array of tokens to validate against. Required if
verifyToken is not provided.(token: string, c: Context) => boolean | Promise<boolean>
Custom function to verify the token. Alternative to providing static
token values.string
default:""
The domain name of the realm, as part of the returned WWW-Authenticate challenge header.
string
default:"Bearer"
The prefix (or schema) for the Authorization header value. If set to empty string, no prefix is expected.
string
default:"Authorization"
The header name to look for the token.
Function
A function to handle hashing for safe comparison of authentication tokens.
string | object | MessageFunction
default:"Unauthorized"
The message returned when no authentication header is provided.
string | object | MessageFunction
default:"Bearer realm=\"\""
The WWW-Authenticate header value when no authentication header is provided.
string | object | MessageFunction
default:"Bad Request"
The message returned when authentication header format is invalid.
string | object | MessageFunction
default:"Bearer error=\"invalid_request\""
The WWW-Authenticate header value when authentication header is invalid.
string | object | MessageFunction
default:"Unauthorized"
The message returned when token is invalid.
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