Skip to content

Instantly share code, notes, and snippets.

@JonForest
Created September 12, 2016 08:45
Show Gist options
  • Save JonForest/d7eaeaee064651b7623e1538907d35a3 to your computer and use it in GitHub Desktop.
Save JonForest/d7eaeaee064651b7623e1538907d35a3 to your computer and use it in GitHub Desktop.
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