Import
Usage
Options
ThebasicAuth middleware accepts a BasicAuthOptions object:
The username for authentication. Required if
verifyUser is not provided.The password for authentication. Required if
verifyUser is not provided.Custom function to verify user credentials. Alternative to providing
username and password.The realm attribute for the WWW-Authenticate header.
The hash function used for secure comparison of credentials.
The message returned when authentication fails. Can be a string, object, or function that returns either.
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