Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Generic basic auth Authorization header field parser

License

Notifications You must be signed in to change notification settings

yanminglife/basic-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

52 Commits

Repository files navigation

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

API

var auth = require('basic-auth')

auth(req)

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.

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.statusCode = 401
 res.setHeader('WWW-Authenticate', 'Basic realm="example"')
 res.end('Access denied')
 } else {
 res.end('Access granted')
 }
})
// Listen
server.listen(3000)

License

MIT

About

Generic basic auth Authorization header field parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%

AltStyle によって変換されたページ (->オリジナル) /