express-authentication-basic

HTTP Basic compatible with express-authentication.

Usage no npm install needed!

<script type="module">
  import expressAuthenticationBasic from 'https://cdn.skypack.dev/express-authentication-basic';
</script>

README

express-authentication-basic

HTTP Basic support compatible with express-authentication.

build status coverage license version downloads

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();
    }
});