Created
December 7, 2011 16:06
-
-
Save fabriziomachado/1443352 to your computer and use it in GitHub Desktop.
application/servers/node_modules/basic-auth/basic-auth.js
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
// 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