Monday 14 December 2020

passport.socket.io cant fix no session found

The Problem
I keep getting the error failed connection to socket.io: No session found using passport.socketio.js and cant find why. I have looked at other similar posts but that doesn't seem to be a problem. Configuration seems fine to me so I guess the problem is in connecting to MongoDB store which is working ok because I can see session data there but only cookie object is getting passed with passport object empty if user is not authenticated or with information if logged in. So session seems ok to me but i cant access the session details with socket.handshake as documented. How do i confirm if user is authenticated in socket.io

EDIT
I tried using this to share sessions but i cant get passport returned only empty cookie object even if user is authenticated there is no passport

 session: Session {
    cookie: { path: '/', _expires: null, originalMaxAge: null, httpOnly: true }
  }

Relevant code

var session = require('express-session')
const MongoStore = require('connect-mongo')(session);
var passportSocketIo = require("passport.socketio");
var cookieParser = require('cookie-parser');

var sessionStore = new MongoStore({ mongooseConnection: mongoose.connection })
app.use(cookieParser())
            
var sessionMiddleware = session({
  secret: '12345678',
  name: 'profile_session',
  resave: true,
  key: 'connect.sid',
  saveUninitialized: true,
  store: sessionStore
})
app.use(sessionMiddleware);

io.use(passportSocketIo.authorize({
    cookieParser: cookieParser,       
    key: 'connect.sid',      
    secret: '12345678',   
    store: sessionStore,        
    success: onAuthorizeSuccess,  
    fail: onAuthorizeFail,    
}));

Mongo DB

session : {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"passport":{}}



from passport.socket.io cant fix no session found

No comments:

Post a Comment