Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Express 4 - Basic HTTP authentication
function authenticate(req, res, next) {
var auth = require('basic-auth');
var user = auth(req);
if (user === undefined || user['name'] !== 'user' || user['pass'] !== 'pass') {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="Bitte anmelden!"');
res.end('Unauthorized');
} else {
next();
}
};
app.get('/admin',authenticate,function(req,res){
// do authenticated things
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment