basic-auth is a focused Node.js module designed for parsing the 'Authorization' header field specifically for Basic HTTP Authentication. It efficiently extracts the username and password from the header string, returning them as an object with `name` and `pass` properties. The current stable version is 2.0.1, indicating a mature and stable package that receives updates primarily for dependency maintenance and minor internal improvements. It operates with a low-cadence release cycle. A key differentiator is its simplicity and direct utility, offering a lightweight solution for a common HTTP parsing task without imposing additional framework or middleware dependencies. This makes it highly versatile for integration into various Node.js HTTP servers, custom middleware, or application logic, handling edge cases such as empty usernames or passwords correctly.
npm install basic-authVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic Node.js HTTP server using `basic-auth` to parse incoming 'Authorization' headers and implement credential validation with `tsscmp` for security.
Change `auth(ctx)` to `auth(ctx.req)` when using with Koa or similar frameworks.
Upgrade Node.js to a modern, supported version. The current LTS is recommended.
Install `tsscmp` (`npm install tsscmp`) and integrate it into your credential checking logic as shown in the package's examples.
For CommonJS environments, use `const auth = require('basic-auth')`. In pure ESM projects, consider a wrapper or use dynamic import `import('basic-auth')` if strictly necessary, but typically this package is used in CJS or transpiled contexts.Always check if `credentials` is defined before attempting to access `credentials.name` or `credentials.pass`. Ensure the client is sending a valid 'Authorization: Basic ...' header.
Ensure you are passing a valid request object, for instance, `http.IncomingMessage` in Node.js, or `ctx.req` if you are using Koa.