Search code examples
node.jsmongooseredispassport.jssession-cookies

Passport: How to use authenticated user on server1 and server2?


I want to share the passport authentication across multiple servers...

Here is my situation:

Server1: (app1.domain.com) authenticates user with passportjs local strategy. using cookie-session

Server2: (app2.domain.com), I want to leverage the user authentication from server1.

I am using the cookie-session middleware like so:

app.use(
  session({
    domain: `.${config.baseDomain}`,
    cookie: {
      path: '/',
      domain: `.${config.baseDomain}` ,
      maxAge: 60000,
    },
    secret: config.secret,
    signed: true,
    resave: true,    
  })
);

On Server1, the resulting req.session object is one line:

 { passport: { user: '5fdb8088ab37a78b980c2e6f' }

On Server2, req.session looks more similar to the req object, with hundred of lines.

I thought that using the same cookie-session middleware would mean that the req.session object would be identical?

I am trying to understand each step, because I am not sure where it is breaking.


Solution

  • The reason the req.session object was different on Server1 and Server2, is because of something that took me a while to track down, though it's painfully simple.

    After isolating cookie-session as the culprit, I finally looked at my package.json to find servers had the different versions of cookie-session installed. (2.0Beta & 1.4.0) Once I installed v1.4.0 on both servers, everything works as expected.