Created
September 12, 2016 08:45
-
-
Save JonForest/d7eaeaee064651b7623e1538907d35a3 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
const basicAuthParser = require('basic-auth') | |
var basicAuth = function (req, res, next) { | |
const user = basicAuthParser(req) | |
const validUser = user && | |
user.name === process.env.BASIC_AUTH_USER && | |
user.pass === process.env.BASIC_AUTH_PASS | |
if (!validUser) { | |
res.set('WWW-Authenticate', 'Basic realm=Authorization Required') | |
return res.sendStatus(401) | |
} | |
next() | |
} | |
module.exports = function enableSimpleAuth(server) { | |
if (process.env.BASIC_AUTH === 'true') server.use(basicAuth) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment