Simple plug-and-play HTTP basic auth middleware for Express. Stable version 1.2.2, occasionally updated. Minimal configuration with static users or custom authorizer functions. Provides timing-attack-safe comparison via safeCompare. Supports synchronous and asynchronous authorization. TypeScript-compatible via included types. Lightweight alternative to passport or express-basic-auth (note: this is a fork).
npm install express-basic-auth-v2Verified import paths — ran on the pinned version, not inferred.
Sets up basic auth middleware with static users, enables challenge response, and logs authenticated user.
Use safeCompare(userInput, secret) instead of safeCompare(secret, userInput).
Add { authorizeAsync: true } to the options object when providing an async authorizer.Access `req.auth` only after ensuring the request passed authorization, or filter later.
Set challenge: true in options to send WWW-Authenticate header triggering browser prompt.
Use 'express-basic-auth' from npm for original package; this package is a fork.
Add { users: { ... } } or { authorizer: myFunction } to the options.Use import basicAuth from 'express-basic-auth' or const basicAuth = require('express-basic-auth').Ensure the authorizer option is a function (or async function) if provided.