Skip to content

Instantly share code, notes, and snippets.

@martin-snajdr
Last active September 5, 2020 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martin-snajdr/f31faae4e6e2ff8b4a83e206efbf2532 to your computer and use it in GitHub Desktop.
Save martin-snajdr/f31faae4e6e2ff8b4a83e206efbf2532 to your computer and use it in GitHub Desktop.
Adding HTTP Basic Auth to WebpackDevServer
// 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