Skip to content

Instantly share code, notes, and snippets.

@ralucas
Last active August 29, 2015 14:05
Show Gist options
  • Save ralucas/c684b9fb11f60cb12385 to your computer and use it in GitHub Desktop.
Save ralucas/c684b9fb11f60cb12385 to your computer and use it in GitHub Desktop.
Setting up basic aut
var basicAuth = require('basic-auth'),
dotenv = require('dotenv');
dotenv.load();
var username = process.env.ADMIN_USERNAME,
password = process.env.ADMIN_PASSWORD;
module.exports = function (req, res, next) {
function unauthorized(res) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.status(401).end();
}
var user = basicAuth(req);
if (!user || !user.name || !user.pass) {
return unauthorized(res);
}
if (user.name === username && user.pass === password) {
return next();
} else {
return unauthorized(res);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment