{"library":"lws-basic-auth","title":"lws-basic-auth Middleware","description":"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.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install lws-basic-auth"],"cli":{"name":"lws","version":null}},"imports":["import BasicAuth from 'lws-basic-auth'; // ESM\n// OR\nconst BasicAuth = require('lws-basic-auth'); // CommonJS"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Lws from 'lws';\nimport BasicAuth from 'lws-basic-auth'; // Assuming ESM compatibility or transpilation\nimport path from 'path';\nimport fs from 'fs';\n\n// Create a dummy file to serve\nconst publicDir = path.join(process.cwd(), 'public');\nconst secretFile = path.join(publicDir, 'secret.html');\n\nif (!fs.existsSync(publicDir)) {\n  fs.mkdirSync(publicDir);\n}\nfs.writeFileSync(secretFile, '<h1>This is a secret page!</h1>', 'utf8');\n\nconst username = process.env.AUTH_USER ?? 'testuser';\nconst password = process.env.AUTH_PASS ?? 'testpass';\n\nconst lws = new Lws();\n\nlws.start({\n  stack: [BasicAuth, 'lws-static'], // Order matters: auth first, then static to protect files\n  directory: publicDir,\n  port: 8000,\n  auth: {\n    user: username,\n    pass: password,\n  },\n}).then(() => {\n  console.log(`lws-basic-auth server running on http://localhost:8000`);\n  console.log(`Access with username: ${username}, password: ${password}`);\n  console.log(`Try http://localhost:8000/secret.html`);\n  console.log(`\nTo stop the server, press Ctrl+C`);\n}).catch(err => {\n  console.error('Failed to start lws:', err);\n  process.exit(1);\n});\n","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}