Skip to content

Instantly share code, notes, and snippets.

@fabriziomachado
Created December 7, 2011 16:06
Show Gist options
  • Save fabriziomachado/1443352 to your computer and use it in GitHub Desktop.
Save fabriziomachado/1443352 to your computer and use it in GitHub Desktop.
application/servers/node_modules/basic-auth/basic-auth.js
// middleware basic authentication
exports.basicAuth = function(req, res, next) {
if (req.headers.authorization && req.headers.authorization.search('Basic ') === 0) {
// Get the username and password
var header = new Buffer(req.headers.authorization.split(' ')[1], 'base64').toString();
header = header.split(":");
var username = header[0];
var password = header[1];
//console.log(request.headers.authorization);
// This is an async that queries the database for the correct credentials
authenticated = authenticateUser(username, password)
if (authenticated) {
next(); return;
} else {
res.send('Authentication required', 401);
}
} else {
res.header('WWW-Authenticate', 'Basic realm="Admin Area"');
res.send('Authentication required', 401);
}
};
function authenticateUser(username, password){
if(username == 'admin' && password=='secret') return true;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment