TransWikia.com

passport.socket.io cant fix no session found

Stack Overflow Asked by Edi_Hadzic on January 11, 2021

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 can’t 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 }
  }

EDIT 2
I noticed in documentation there might be a CORS error which I experienced before and that could be a reason for cookie not sending as I understood. I had to change io on server side because I had CORS error I don’t know if this could be connected in any way. changing localhost to 127.0.0.1 refuses to connect.

const io = require('socket.io')(http, {
    cors: {
        origin: "http://localhost:3000",
        methods: ["GET", "POST"]
    }
});

This is part of socket.request as you can see there is no sessionID.

cleanup: [Function: cleanup],
  socketio_version_1: true,
  cookie: [Object: null prototype] {},
  sessionID: '',
  user: { logged_in: false }

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":{}}

2 Answers

Ok i got it
At the end it was a CORS error and i managed to fix it using this post. Now i am having trouble with store not updating when i try to log out and making me stay always logged in but oh well

https://github.com/jfromaniello/passport.socketio/issues/95

Correct answer by Edi_Hadzic on January 11, 2021

Handshake data is no longer stored in the same place. Not socket.handshake.user but socket.request.user. Also you need to use the ip address (127.0.0.1) instead of localhost. So you go to 127.0.0.1:PORT instead of localhost:PORT

Answered by Someone21 on January 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP