opentok
Version:
OpenTok server-side SDK
79 lines (58 loc) • 2.07 kB
Markdown
for whatever.
```
$ npm install basic-auth
```
```js
var auth = require('basic-auth')
```
Get the basic auth credentials from the given request. The `Authorization`
header is parsed and if the header is invalid, `undefined` is returned,
otherwise an object with `name` and `pass` properties.
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`.
```js
var auth = require('basic-auth');
var user = auth(req);
// => { name: 'something', pass: 'whatever' }
```
```js
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.statusCode = 401
res.setHeader('WWW-Authenticate', 'Basic realm="example"')
res.end('Access denied')
} else {
res.end('Access granted')
}
})
// Listen
server.listen(3000)
```
[ ](LICENSE)
[ ]: https://img.shields.io/npm/v/basic-auth.svg
[ ]: https://npmjs.org/package/basic-auth
[ ]: https://img.shields.io/node/v/basic-auth.svg
[ ]: https://nodejs.org/en/download
[ ]: https://img.shields.io/travis/jshttp/basic-auth/master.svg
[ ]: https://travis-ci.org/jshttp/basic-auth
[ ]: https://img.shields.io/coveralls/jshttp/basic-auth/master.svg
[ ]: https://coveralls.io/r/jshttp/basic-auth?branch=master
[ ]: https://img.shields.io/npm/dm/basic-auth.svg
[ ]: https://npmjs.org/package/basic-auth
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Node.js Version][node-version-image]][node-version-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
Generic basic auth Authorization header field parser