Created
August 4, 2017 06:40
-
-
Save cookie-ag/8242bc8c2853329a349eaefe7285ade9 to your computer and use it in GitHub Desktop.
HTTP auth in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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