Created
May 7, 2015 16:20
-
-
Save chrisbodhi/679b34c6fdb163ad33aa to your computer and use it in GitHub Desktop.
Basic Auth Middleware
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
| var auth = require('basic-auth'); | |
| /** | |
| * Simple basic auth middleware for use with Express 4.x. | |
| * via http://www.danielstjules.com/2014/08/03/basic-auth-with-express-4/ | |
| * | |
| * @param {string} username Expected username | |
| * @param {string} password Expected password | |
| * @returns {function} Express 4 middleware requiring the given credentials | |
| */ | |
| var basicAuth = function(username, password) { | |
| return function(req, res, next) { | |
| var user = auth(req); | |
| if (!user || user.name !== username || user.pass !== password) { | |
| res.set('WWW-Authenticate', 'Basic realm=Authorization Required'); | |
| res.sendStatus(401); | |
| } | |
| next(); | |
| }; | |
| }; | |
| module.exports = basicAuth; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment