简体   繁体   中英

passport session with websocket server library

Could passport session be expected to work with a web socket library( https://github.com/websockets/ws )? We have a regular passport authentication setup: but the req.session.user sometimes shows up correctly in the websocket request and sometimes it does not.

We are initializing the websocket like this:

this.wss = new WebSocketServer({
  server: this.server, // This is the express app
  verifyClient: function(info, done) {
    self.session_parser(info.req, {}, function() {
      console.log('VERIFY ', info.req.session);
      // allow connection only if session is valid and a user is logged in
      done(info.req.session && info.req.session.passport &&
        info.req.session.passport.user &&
        info.req.session.passport.user.id);
    });
  }
});

The problems is that info.req.session.user shows up correctly for some web socket requests and sometimes it does not in the verifyClient function. req.session always shows up correctly.

It is in dev env?

If you use memorystore session it cleaned after app reload. You can switch to session-file-store but it usually don't store on res.redirect()

In other cases you must check is session don't expire on server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM