hairmot
hairmot

Reputation: 2965

BrowserSync share session

I'm wanting to use BrowserSync for some testing and development on a COTS (commercial, of the shelf) system - think like Sharepoint, but it's not Sharepoint.

As this is a COTS system, one of the security features that we cannot disable is that it will only allow one active session per user id. Having multiple browsers synced and trying to login will fail as the COTS system detects more than one user login.

Is there any way to have browser sync treat a window/browser as the 'master' session and simply re-draw the 'slaves' using the response from the master window? As opposed to copying all actions across and causing multiple requests to be sent from different browsers?

Upvotes: 2

Views: 522

Answers (1)

Peter
Peter

Reputation: 1822

I had the same issue here is my solution (I save cookies in BS and use it all the time, its works well for me):

gulp.task('browser-sync', function () {
    var cookies = {};

    browserSync.init({
        proxy: {
            target: "localhost",
            proxyReq: [
                function (proxyReq) {
                    var cookieText = Object.keys(cookies).map(function (name) {
                        return name + '=' + cookies[name];
                    }).join('; ')
                    if (proxyReq._headers.cookie) {
                        proxyReq.setHeader('cookie', cookieText);
                    }
                }
            ],
            proxyRes: [
                function (proxyRes, req, res) {
                    if (proxyRes.headers && proxyRes.headers['set-cookie']) {
                        proxyRes.headers['set-cookie'].forEach(function (cookie) {
                            var name, value;
                            var t = cookie.split(';')[0].split('=');
                            name = t[0];
                            value = t[1];
                            cookies[name] = value;
                        });
                    }
                }
            ]
        }
    });
});

Upvotes: 2

Related Questions