Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Works in express but No session found error #146

Open
tzvc opened this issue Dec 6, 2019 · 1 comment
Open

Works in express but No session found error #146

tzvc opened this issue Dec 6, 2019 · 1 comment

Comments

@tzvc
Copy link

tzvc commented Dec 6, 2019

Hi there,

I have an issue, when I try to connect my websocket through passport.socket.io I keep getting a no session found error. However, when I look at the log, it seems that i'm receiving the session cookie properly and it is valid (the same session is used in express and it work there)

What am I missing here?

My server code:

const SESSION_SECRET = "coolkidsrunlinux";
const SESSION_ID_NAME = "session_cookie";

try {
        var sessionStore = new MongoDBStore({
                uri: "mongodb://localhost/connect_mongodb_session_test",
                collection: "mySessions"
        });
} catch (error) {
        console.error("Error setting up mongoDB session store");
        throw error;
}

app.use(cookieparser());
app.use(
        session({
                secret: SESSION_SECRET,
                name: SESSION_ID_NAME,
                key: SESSION_ID_NAME,
                store: sessionStore,
                proxy: true,
                resave: false,
                saveUninitialized: true
        })
);

io.use((socket, next) => {
        console.log(JSON.stringify(socket.handshake));
        return next();
});

io.use(
        passportSocketIo.authorize({
                cookieParser: cookieparser, // the same middleware you registrer in express
                key: SESSION_ID_NAME, // the name of the cookie where express/connect stores its session_id
                secret: SESSION_SECRET, // the session_secret to parse the cookie
                store: sessionStore, // we NEED to use a sessionstore. no memorystore please
                success: onAuthorizeSuccess, // *optional* callback on success - read more below
                fail: onAuthorizeFail // *optional* callback on fail/error - read more below
        })
);

my client code (dart)

      var wsOptions = <String, dynamic>{
        'transports': ['websocket'],
        'query': "conversation_id=${_conversation.id}",
        'extraHeaders': {
          "Cookie": "session_cookie=${cookies["session_cookie"]}",
          "conversation_id": _conversation.id,
        }
      };
      print(wsOptions);
      _socket = io("$API_ROOT", wsOptions);

Upon connection, when I log the socket.handshake i get this which seems to indicate that the session is received

{"headers":{"host":"api.join.under-wolf.eu","user-agent":"Dart/2.5 (dart:io)","cache-control":"no-cache","accept-encoding":"gzip","sec-websocket-key":"57GDhElwq8cgONQNtjqQxg==","session_cookie":"s%3AEDQJv_-3_k6MIKQyESfQ567Q7xFqpx_8.5qCt02EusyXpsoPZJSllVxiuq%2B7I3uvXeIOKxmNuIdo","conversation_id":"5dea5627ade2fa0012d67916","cookie":"session_cookie=s%3AEDQJv_-3_k6MIKQyESfQ567Q7xFqpx_8.5qCt02EusyXpsoPZJSllVxiuq%2B7I3uvXeIOKxmNuIdo, session_cookie=s%3AEDQJv_-3_k6MIKQyESfQ567Q7xFqpx_8.5qCt02EusyXpsoPZJSllVxiuq%2B7I3uvXeIOKxmNuIdo","sec-websocket-version":"13","sec-websocket-extensions":"permessage-deflate; client_max_window_bits","x-forwarded-for":"91.170.175.80","x-forwarded-host":"api.join.under-wolf.eu","x-forwarded-server":"api.join.under-wolf.eu","connection":"Keep-Alive","content-length":"0"},"time":"Fri Dec 06 2019 16:33:07 GMT+0000 (Coordinated Universal Time)","address":"::ffff:127.0.0.1","xdomain":false,"secure":false,"issued":1575649987158,"url":"/socket.io/?conversation_id=5dea5627ade2fa0012d67916&EIO=3&transport=polling","query":{"conversation_id":"5dea5627ade2fa0012d67916","EIO":"3","transport":"polling"}}
@cmarrero01
Copy link

Hi @theochampion you resolve this issue? I have the exact same error here. I notice that I don't have anything on the session collection so I suppose that is the problem of MongoStore and not an issue of passport.socketio but I don't know.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants