JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 9059062
  • Score
    100M100P100Q263852F
  • License MIT

node.js basic auth parser

Package Exports

  • basic-auth
  • basic-auth/package.json

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (basic-auth) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

basic-auth

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Generic basic auth Authorization header field parser for whatever.

Installation

$ npm install basic-auth

Example

Pass a node request or koa Context object to the module exported. If parsing fails undefined is returned, otherwise an object with .name and .pass.

var auth = require('basic-auth');
var user = auth(req);
// => { name: 'something', pass: 'whatever' }

With vanilla node.js http server

var http = require('http')
var auth = require('basic-auth')

// Create server
var server = http.createServer(function(req, res){
  var credentials = auth(req)

  if (!credentials || credentials.name !== 'john' || credentials.pass !== 'secret') {
    res.writeHead(401, {
      'WWW-Authenticate': 'Basic realm="example"'
    })
    res.end()
  } else {
    res.end('Access granted');
  }
})

// Listen
server.listen(3000)

License

MIT