Skip to content

Instantly share code, notes, and snippets.

@dommmel
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dommmel/0b2760359b42ed62c9d8 to your computer and use it in GitHub Desktop.
Save dommmel/0b2760359b42ed62c9d8 to your computer and use it in GitHub Desktop.
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