Import
Usage
Options
ThebasicAuth middleware accepts a BasicAuthOptions object:
string
The username for authentication. Required if
verifyUser is not provided.string
The password for authentication. Required if
verifyUser is not provided.(username: string, password: string, c: Context) => boolean | Promise<boolean>
Custom function to verify user credentials. Alternative to providing
username and password.string
default:"Secure Area"
The realm attribute for the WWW-Authenticate header.
Function
The hash function used for secure comparison of credentials.
string | object | MessageFunction
default:"Unauthorized"
The message returned when authentication fails. Can be a string, object, or function that returns either.
(c: Context, username: string) => void | Promise<void>
Callback function called on successful authentication. Useful for setting user context.
Signature
Examples
With onAuthSuccess callback
With custom verifyUser function
Multiple users
Behavior
- Returns 401 Unauthorized if credentials are missing or invalid
- Sets
WWW-Authenticateheader with the specified realm - Uses timing-safe comparison to prevent timing attacks
- Supports custom hash functions for secure credential comparison
- Can validate multiple username/password pairs