0

I have Express with Passport authentication and have added session to it as below.

app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser()); // get information from html forms

app.set('view engine', 'ejs'); // set up ejs for templating

// required for passport
// Trust Proxy as it is behind web server.
//app.set('trust proxy', 1);

app.use(session({
    secret: 'this is secret',
    resave: false,
    store: new MongoStore({ url: configDB.url }),
    saveUninitialized: true,
    cookie: { httpOnly: true, maxAge: 2419200000 }
})); // session secret

app.get("/*", function(req, res, next) {
    if (typeof req.cookies['connect.sid'] !== 'undefined') { console.log(req.cookies['connect.sid']); }
    next(); // call the next middleware
});

app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session

Not sure what the issue is, in Passport login I am setting session name as req.session.name. It works fine if User A logged in, but when User B logs in, it doesn't create a new Session Object, but overwrites User A session object with User B including passport details. Also in browser the cookies get reset to User B, which should be obvious.

laggingreflex
  • 32,948
  • 35
  • 141
  • 196
skumar
  • 41
  • 6
  • May be lack of my knowledge, but what I found it works fine if I use different browser, I guess cookie once set in browser it is for all tabs unless you logout. I inspected the cookie in each tab and found that they are present even if the user is not logged and hence on request send it sends the cookie (session id) and server recognize as present and only switches user. But I am sure there would be way to define the cookie scope. Any more insight will be greatly apperciated! – skumar Mar 21 '15 at 11:02
  • `req.session` depends on the client (browser + cookies), not the Passport user. You should use `req.user`, which is provided by Passport, if you want to store user dependent session data. – laggingreflex Mar 22 '15 at 15:55

0 Answers0