Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add cookies to a webpack dev server proxy?

I'm trying to set up a proxy within my webpack dev server. The issue is that I don't control the server I'm connecting to, and I need to authenticate the request.

Is there a way I can add cookies on to the request I send to the proxy server? I've looked through the webpack dev server proxy server page, and the node-http-proxy page it links to, and I don't see any mention of cookies. I'm also not sure if there's a way for me to see these forwarded requests, so I can't tell if anything I'm trying is doing anything.

Any ideas?

like image 856
fnsjdnfksjdb Avatar asked Jan 17 '16 05:01

fnsjdnfksjdb


People also ask

What is the use of webpack-dev-server?

webpack-dev-server can be used to quickly develop an application. See the development guide to get started.

Is the default port for webpack Dev what server?

The Result Either method will start a server instance and begin listening for connections from localhost on port 8080 . webpack-dev-server is configured by default to support live-reload of files as you edit your assets while the server is running. See the documentation for more use cases and options.


2 Answers

If you need to only rewrite the cookie domain for the proxy, check out the option cookieDomainRewrite in node-http-proxy.

Additionally if you wanted to find a way to inject in custom behavior around cookies on requests / responses, then check out the events you can hook in to:

proxy.on('proxyRes', function (proxyRes, req, res) {
    console.log('RAW Response from the target',JSON.stringify(proxyRes.headers, true, 2));
});


proxy.on('proxyReq', function (proxyRes, req, res) {
    console.log('RAW Request from the target',JSON.stringify(proxyReq.headers, true, 2));
});

https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events

These options can be added to the webpack.config.js for the devServer proxy, like this:

{
    devServer: {
        proxy: {
            onProxyReq: function(proxyReq, req, res){
                proxyReq.setHeader('x-added', 'foobar');
            },
            cookieDomainRewrite: ""
        }
    }
}

https://github.com/chimurai/http-proxy-middleware#http-proxy-events

like image 183
Leon Gibat Avatar answered Sep 18 '22 21:09

Leon Gibat


After looking into this further, it does seem like the dev server will just forward any cookies you send it. Didn't work for the authentication I was trying to do, I guess Amazon has some more security in place that I couldn't account for, but that is the answer.

Add cookies to the request you're sending to the dev server, and set up the proxy properly.

like image 38
fnsjdnfksjdb Avatar answered Sep 17 '22 21:09

fnsjdnfksjdb