Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding HTTP Basic Auth to Webpack Dev Server

Is there a simple way to add simple HTTP Basic Authentication to the Webpack Dev Server in a Zero-Configuration-React-App.

What I did:

  1. I've created a react app using create-react-app, then built my whole app.
  2. I then ejected it using npm run eject.
  3. Then I googled for hours and didn't find any hint.

Is there a configuration for this or do I have to add a middleware? Or is the only / easiest way to use webpack together with express?

like image 423
Martin Snajdr Avatar asked Jun 08 '26 19:06

Martin Snajdr


2 Answers

hi yes you can please look at setup key in webpack config or this url https://webpack.js.org/configuration/dev-server/#devserver-setup i have been there facing such issues, you can simply use basic-auth package to overcome your need

like image 123
Khalid Hussain Avatar answered Jun 10 '26 07:06

Khalid Hussain


A really easy way to do this is to use the express-basic-auth middleware.

Install it as a dev dependency:

npm install --save-dev express-basic-auth

Then configure webpack as follows:

const basicAuth = require('express-basic-auth');

module.exports = {
  ...
  devServer: {
    ...
    setupMiddlewares: (middlewares, devServer) => {
      devServer.app.use(basicAuth({
        users: {
            'user1': 'password1',
            'user2': 'password2'
        },
        challenge: true
      }));

      return middlewares;
    }
  }
}

Or however you want it to work.

like image 35
Dan King Avatar answered Jun 10 '26 08:06

Dan King



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!