Skip to content

Instantly share code, notes, and snippets.

@xcyxiner
Forked from JonForest/Basic Auth on Loopback
Created February 13, 2019 02:54
Show Gist options
  • Save xcyxiner/7d37ccb6c458957423dd1448a96b0cce to your computer and use it in GitHub Desktop.
Save xcyxiner/7d37ccb6c458957423dd1448a96b0cce 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