Connect HTTP Authentication Middleware

1.0.6 · maintenance · verified Tue Apr 21

http-auth-connect provides a specialized middleware adapter that integrates the robust `http-auth` module with the `Connect` framework, thereby extending its utility to widely-used web frameworks like `Express.js`. It enables straightforward implementation of HTTP Basic and Digest authentication within Node.js web applications by wrapping `http-auth` configurations into a `Connect`-compatible middleware function. The current stable version is 1.0.6, with recent updates primarily focused on addressing security audit findings, indicating a maintenance-oriented release cadence rather than active feature development. Its primary differentiator is simplifying the application of `http-auth`'s comprehensive authentication schemes directly within the standard `Connect`/`Express` middleware stack, allowing developers to leverage existing, proven authentication logic without custom integration boilerplate.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up HTTP Basic Authentication using `http-auth-connect` with Express, protecting a simple route. It includes creating a temporary .htpasswd file for user credentials.

const express = require('express');
const auth = require('http-auth');
const authConnect = require('http-auth-connect');
const path = require('path');
const fs = require('fs');

// Create a dummy htpasswd file for demonstration
const htpasswdPath = path.join(__dirname, 'users.htpasswd');
fs.writeFileSync(htpasswdPath, 'gevorg:$apr1$D9ByKj6f$d2yF/X7zN4L9P.hR/oE3w1\nSarah:$apr1$uL9P.hR/oE3w1$D9ByKj6f$d2yF/X7zN4L9P.hR/oE3w1\n');

const basic = auth.basic({
  realm: 'Secure Area',
  file: htpasswdPath // Path to .htpasswd file (gevorg:gpass, Sarah:testpass)
});

const app = express();

// Apply the authentication middleware
app.use(authConnect(basic));

// Setup a protected route
app.get('/', (req, res) => {
  res.send(`Hello from express - ${req.user}! You are authenticated.`);
});

// Start server
const PORT = process.env.PORT || 1337;
app.listen(PORT, () => {
  console.log(`Server running at http://127.0.0.1:${PORT}/`);
  console.log('Try accessing with user "gevorg" and password "gpass", or "Sarah" and "testpass".');
});

// Clean up dummy file on exit (for proper demonstration)
process.on('exit', () => {
    if (fs.existsSync(htpasswdPath)) {
        fs.unlinkSync(htpasswdPath);
    }
});

view raw JSON →