Last active
August 29, 2015 14:01
Express 4 - Basic HTTP authentication
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
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