0

I am trying to retrieve user data after a successful connection with Passport.js.

I use express-session and express-socket.io-session in order to share data. Here is a part of my code :

var expressSession   = require('express-session');
var sharedsession    = require("express-socket.io-session");
var MongoStore       = require('connect-mongo')(expressSession);
var sessionStore     = new MongoStore({ 
    url: 'mongodb://127.0.0.1:27017/default',
    ttl: 24 * 60 * 60 // = 1 day
});

var session = expressSession({
    cookie: cookieParser,
    secret: 'mySecret',
    resave: false,
    saveUninitialized: false,
    store: sessionStore,
});

app.use(session);
app.use(passport.initialize());
app.use(passport.session());

io.use(sharedsession(session, {
    autosave: true
}))
require('./app/socket-io.js')(io);

Here is where I try to save some data (req.session.test).

app.get( '/index.html', isLoggedIn, function( req, res ) {
    req.session.test = 'Hello World !';
    console.log('=====');
    console.log(req.sessionID);
    console.log(req.session);
    res.sendFile( root + '/index.html' );
});

I noticed that req.sessionID inside app.get() is different from client.request.sessionID inside io.on('connection, function(...)). This could be important according to the comments in this thread : How to share sessions with Socket.IO 1.x and Express 4.x?

I would be happy for any help ! :)

Community
  • 1
  • 1
Arpp
  • 301
  • 1
  • 11

1 Answers1

1

Actually, this was a CORS issue.

On client side, I connected to socket.io with :

var socket = io.connect("127.0.0.1"); // with the server ip

This triggered cross origin requests, and limited the behaviour of passport.socketio (https://github.com/jfromaniello/passport.socketio). Look at the "CORS-Workaround" section for more information.

Then, the solution was simply to follow the "workaround" :

var socket = io.connect('//' + window.location.host);
Arpp
  • 301
  • 1
  • 11