Last active
September 5, 2020 10:02
-
-
Save martin-snajdr/f31faae4e6e2ff8b4a83e206efbf2532 to your computer and use it in GitHub Desktop.
Adding HTTP Basic Auth to WebpackDevServer
This file contains 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
// around line 25... | |
var config = require('../config/webpack.config.dev'); | |
var paths = require('../config/paths'); | |
var auth = require('basic-auth'); | |
//... | |
function runDevServer(host, port, protocol) { | |
var devServer = new WebpackDevServer(compiler, { | |
//around line 265... | |
host: host, | |
setup: function(app) { | |
app.all('*', function (req, res, next) { | |
if (process.env.AUTH_USER && process.env.AUTH_PASSWORD) { | |
var credentials = auth(req) | |
if (!credentials || credentials.name !== process.env.AUTH_USER || credentials.pass !== process.env.AUTH_PASSWORD) { | |
res.statusCode = 401 | |
res.setHeader('WWW-Authenticate', 'Basic realm="Prototype Access"') | |
res.end('Access denied') | |
} else { | |
next() | |
} | |
} else { | |
next() | |
} | |
}) | |
} | |
}); | |
// Our custom middleware proxies requests to /index.html or a remote API. | |
addMiddleware(devServer); | |
//... | |
} | |
//... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment