lws-basic-auth Middleware

2.0.0 · maintenance · verified Sun Apr 19

lws-basic-auth is a middleware plugin designed to password-protect local-web-server (lws) instances using HTTP Basic Authentication. It integrates directly into the `lws` command-line and programmatic configuration, allowing users to define a username and password to restrict access to the served content. The current stable version is 2.0.0. While `lws` itself has seen infrequent updates, this middleware provides a focused and lightweight solution specifically for the `lws` ecosystem. It differentiates itself from general-purpose basic authentication libraries by being tightly coupled with `lws`'s plugin architecture, offering a streamlined setup for securing local development servers. Its release cadence is infrequent, suggesting it is a stable package in maintenance mode, receiving minimal updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up an `lws` server with `lws-basic-auth` middleware, protecting a static HTML file. It demonstrates both programmatic setup with environment variables for credentials and accessing the protected resource.

import Lws from 'lws';
import BasicAuth from 'lws-basic-auth'; // Assuming ESM compatibility or transpilation
import path from 'path';
import fs from 'fs';

// Create a dummy file to serve
const publicDir = path.join(process.cwd(), 'public');
const secretFile = path.join(publicDir, 'secret.html');

if (!fs.existsSync(publicDir)) {
  fs.mkdirSync(publicDir);
}
fs.writeFileSync(secretFile, '<h1>This is a secret page!</h1>', 'utf8');

const username = process.env.AUTH_USER ?? 'testuser';
const password = process.env.AUTH_PASS ?? 'testpass';

const lws = new Lws();

lws.start({
  stack: [BasicAuth, 'lws-static'], // Order matters: auth first, then static to protect files
  directory: publicDir,
  port: 8000,
  auth: {
    user: username,
    pass: password,
  },
}).then(() => {
  console.log(`lws-basic-auth server running on http://localhost:8000`);
  console.log(`Access with username: ${username}, password: ${password}`);
  console.log(`Try http://localhost:8000/secret.html`);
  console.log(`
To stop the server, press Ctrl+C`);
}).catch(err => {
  console.error('Failed to start lws:', err);
  process.exit(1);
});

view raw JSON →