Basic Auth Middleware
The package `basic-auth-middleware` provides a foundational HTTP Basic Authentication middleware for Node.js `http` servers. Released as version 1.0.0, this package is definitively abandoned, having received its last update over seven years ago (as of April 2026). It was marked with an "experimental" stability badge even at its stable release, indicating it was never intended for long-term production use. It integrates with native `http` server requests and responses, employing a callback-based API (`middleware(req, res, ctx, done)`) for authentication flow control. Error objects are constructed using the `boom` library, which is itself deprecated. Due to its unmaintained status and dated design patterns, it is not suitable for new projects and poses potential security and maintenance risks for existing applications.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module ... not supported.
cause Attempting to import this CommonJS-only package using ES Modules (`import`) syntax in a modern Node.js environment.fixUse CommonJS `require` syntax: `const Auth = require('basic-auth-middleware');` -
TypeError: auth is not a function
cause Attempting to call the imported module directly as a middleware function instead of first instantiating it with credentials.fixEnsure you create an instance by calling `Auth` with username and password: `const auth = Auth('your-username', 'your-password');` before using `auth(req, res, ctx, done);`
Warnings
- breaking This package is considered abandoned, with the last update over 7 years ago (as of April 2026). It also carried an 'experimental' stability badge even at its 1.0.0 release. It is not recommended for new projects and should be replaced in existing ones due to potential security vulnerabilities and lack of maintenance.
- gotcha The middleware uses a callback-based API (`middleware(req, res, ctx, done)`) which is less common in modern async/await-centric Node.js development. This can lead to more complex error handling and control flow compared to promise-based alternatives.
- gotcha This package relies on the 'boom' library for error objects. 'boom' is also deprecated and largely unmaintained. Consumers of the `err` object will need to handle `boom`-specific properties (e.g., `err.output.statusCode`) rather than standard JavaScript `Error` properties.
Install
-
npm install basic-auth-middleware -
yarn add basic-auth-middleware -
pnpm add basic-auth-middleware
Imports
- Auth
import Auth from 'basic-auth-middleware'
const Auth = require('basic-auth-middleware')
Quickstart
const Auth = require('basic-auth-middleware');
const http = require('http');
const USERNAME = process.env.BASIC_AUTH_USERNAME || 'my-username';
const PASSWORD = process.env.BASIC_AUTH_PASSWORD || 'some-password';
const PORT = process.env.PORT || 3000;
const auth = Auth(USERNAME, PASSWORD);
const server = http.createServer(function (req, res) {
const ctx = {}; // Context object, can be used for passing data
auth(req, res, ctx, function (err) {
if (err) {
// boom errors typically have .output.statusCode
res.statusCode = err.output ? err.output.statusCode : 401;
res.setHeader('WWW-Authenticate', 'Basic realm="Authentication Required"');
res.end('Not authenticated. Please provide valid credentials.');
console.log('Authentication failed.');
return;
}
res.end('Authentication successful! Welcome.');
console.log('Authentication successful.');
});
});
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log(`Try accessing with 'curl -u ${USERNAME}:${PASSWORD} http://localhost:${PORT}'`);
console.log(`Or without credentials to fail: 'curl http://localhost:${PORT}'`);
});