3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Node.js Version][node-version-image]][node-version-url]
6 [![Build Status][travis-image]][travis-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
9 Generic basic auth Authorization header field parser for whatever.
14 $ npm install basic-auth
20 var auth = require('basic-auth')
25 Get the basic auth credentials from the given request. The `Authorization`
26 header is parsed and if the header is invalid, `undefined` is returned,
27 otherwise an object with `name` and `pass` properties.
31 Pass a node request or koa Context object to the module exported. If
32 parsing fails `undefined` is returned, otherwise an object with
36 var auth = require('basic-auth');
38 // => { name: 'something', pass: 'whatever' }
42 ### With vanilla node.js http server
45 var http = require('http')
46 var auth = require('basic-auth')
49 var server = http.createServer(function (req, res) {
50 var credentials = auth(req)
52 if (!credentials || credentials.name !== 'john' || credentials.pass !== 'secret') {
54 res.setHeader('WWW-Authenticate', 'Basic realm="example"')
55 res.end('Access denied')
57 res.end('Access granted')
69 [npm-image]: https://img.shields.io/npm/v/basic-auth.svg
70 [npm-url]: https://npmjs.org/package/basic-auth
71 [node-version-image]: https://img.shields.io/node/v/basic-auth.svg
72 [node-version-url]: https://nodejs.org/en/download
73 [travis-image]: https://img.shields.io/travis/jshttp/basic-auth/master.svg
74 [travis-url]: https://travis-ci.org/jshttp/basic-auth
75 [coveralls-image]: https://img.shields.io/coveralls/jshttp/basic-auth/master.svg
76 [coveralls-url]: https://coveralls.io/r/jshttp/basic-auth?branch=master
77 [downloads-image]: https://img.shields.io/npm/dm/basic-auth.svg
78 [downloads-url]: https://npmjs.org/package/basic-auth