express-authentication-basic
v0.3.1
Published
HTTP Basic compatible with express-authentication.
Downloads
50
Maintainers
Readme
express-authentication-basic
HTTP Basic support compatible with express-authentication.
It's pretty cool.
With express-authentication:
var express = require('express'),
authentication = require('express-authentication'),
basic = require('express-authentication-basic');
var app = express(),
auth = authentication(),
login = basic(function(challenge, callback) {
if (challenge.username === 'admin' && challenge.password === 'secret') {
callback(null, true, { user: 'charles' });
} else {
callback(null, false, { error: 'INVALID_PASSWORD' });
}
});
app.use(auth);
app.use(login);
app.get('/', authentication.by(login).required(), function(req, res) {
var who = authentication.for(login).of(req);
res.status(200).send({ user: who.user });
});
Standalone:
var express = require('express'),
basic = require('express-authentication-basic');
var app = express(),
login = basic(function(challenge, callback) {
if (challenge.username === 'admin' && challenge.password === 'secret') {
callback(null, true, { user: 'charles' });
} else {
callback(null, false, { error: 'INVALID_PASSWORD' });
}
});;
app.use(login);
app.get('/', function(req, res) {
if (req.authenticated) {
res.status(200).send({ user: req.authentication.user });
} else {
res.status(401).send();
}
});