Registry / auth-security / express-basic-auth-v2

express-basic-auth-v2

JSON →
library1.2.2jsnpmunverified

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-v2
INSTALL
IMPORT
SIG · EXPRESS-BASIC-AUTH
E
express-basic-auth-v2
auth-securityjavascriptv1.2.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import basicAuth from 'express-basic-auth'
const basicAuth = require('express-basic-auth')
Default import in ESM; CJS require also works. Package does not export named exports besides safeCompare.
safeCompare (named export)
import basicAuth, { safeCompare } from 'express-basic-auth'
const { safeCompare } = require('express-basic-auth')
safeCompare is a named export; accessible via destructured import or via basicAuth.safeCompare.
basicAuth.AuthorizedResponseCallback
import type { AuthorizedResponseCallback } from 'express-basic-auth'
TypeScript type import for async authorizer callback signature. Only available when using ESM.

Sets up basic auth middleware with static users, enables challenge response, and logs authenticated user.

import express from 'express'; import basicAuth from 'express-basic-auth'; const app = express(); app.use(basicAuth({ users: { 'admin': 'supersecret' }, challenge: true })); app.get('/', (req, res) => { console.log(req.auth.user); res.send('Hello Authenticated!'); }); app.listen(3000);
Debug
Known issues
gotchasafeCompare should always receive user input as first argument to prevent timing attacks.
fix
Use safeCompare(userInput, secret) instead of safeCompare(secret, userInput).
affects: >=0.0.0
gotchaWhen using custom authorizer with async, you must pass authorizeAsync: true in options.
fix
Add { authorizeAsync: true } to the options object when providing an async authorizer.
affects: >=0.0.0
gotchaThe middleware adds `req.auth` even for unauthorized requests; check authorization status separately.
fix
Access `req.auth` only after ensuring the request passed authorization, or filter later.
affects: >=0.0.0
gotchaDefault response body for 401 is empty; clients may not display a prompt without challenge option.
fix
Set challenge: true in options to send WWW-Authenticate header triggering browser prompt.
affects: >=0.0.0
deprecatedPackage name 'express-basic-auth-v2' may be confused with original 'express-basic-auth' by LionC.
fix
Use 'express-basic-auth' from npm for original package; this package is a fork.
affects: >=0.0.0
Errors
Common errors & fixes
Error: No users or authorizer function specified
Missing required option 'users' or 'authorizer' in options object.
fix
Add { users: { ... } } or { authorizer: myFunction } to the options.
TypeError: basicAuth is not a function
Improper import: using named import instead of default import.
fix
Use import basicAuth from 'express-basic-auth' or const basicAuth = require('express-basic-auth').
Error: Authorizer must be a function
Passed a non-function as authorizer option.
fix
Ensure the authorizer option is a function (or async function) if provided.
Upgrade
Version history
1.2.2latest on npm
Audit
Dependencies
expressrequiredRequired peer dependency; middleware is designed to work with Express applications.
Agent activity
19 hits · last 30 days
node
16
Amazon
1
OpenAI (training)
1
Resources
express-basic-auth-v2 — npm install express-basic-auth-v2 · libregistry