Last active
August 29, 2015 14:05
-
-
Save ralucas/c684b9fb11f60cb12385 to your computer and use it in GitHub Desktop.
Setting up basic aut
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
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