Skip to content

Instantly share code, notes, and snippets.

@cookie-ag
Created August 4, 2017 06:40
Show Gist options
  • Save cookie-ag/8242bc8c2853329a349eaefe7285ade9 to your computer and use it in GitHub Desktop.
Save cookie-ag/8242bc8c2853329a349eaefe7285ade9 to your computer and use it in GitHub Desktop.
HTTP auth in Node.js
'use strict';
var basic_Auth = require('basic-auth');
//HTTP Authentication
exports.enableAuth = function(req, res, next) {
function unauthorized(res) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.sendStatus(401);
}
var user = basic_Auth(req);
if (!user || !user.name || !user.pass) {
return unauthorized(res);
}
if (user.name === '' && user.pass === '') {
return next();
} else {
return unauthorized(res);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment