-
-
Save xcyxiner/7d37ccb6c458957423dd1448a96b0cce 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