Created
August 27, 2016 22:30
-
-
Save Echooff3/019271c7418d20043bea6e5ab0764517 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Your utility library for express | |
*/ | |
var basicAuth = require('basic-auth'); | |
/** | |
* Simple basic auth middleware for use with Express 4.x. | |
* | |
* @example | |
* app.use('/api-requiring-auth', utils.basicAuth('username', 'password')); | |
* | |
* @param {string} username Expected username | |
* @param {string} password Expected password | |
* @returns {function} Express 4 middleware requiring the given credentials | |
*/ | |
exports.basicAuth = function(username, password) { | |
return function(req, res, next) { | |
var user = basicAuth(req); | |
if (!user || user.name !== username || user.pass !== password) { | |
res.set('WWW-Authenticate', 'Basic realm=Authorization Required'); | |
return res.sendStatus(401); | |
} | |
next(); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment