express-socket.io-session: Undefined
Trying to let the code speak for itself.
var express = require("express");
var app = express();
var server = require("http").createServer(app);
var io = require("socket.io")(server);
var session = require('express-session')({
name: "session",
secret: "lfdod9Jiru65MMl",
saveUninitialized: true,
resave: true,
cookie: {
httpOnly: false,
maxAge: 1000 * 60 * 60 * 24
}
});
var sharedsession = require("express-socket.io-session");
app.use(session);
io.use(sharedsession(session), {
autoSave:true
});
io.on('connection', function(socket){
socket.on("clicked", function(value) {
// this does happen, I checked with console.logs
socket.handshake.session.key = value;
});
});
`app.get("/index", function(req, res) {
console.log("xxx");
console.log(req.session.key); //writes undefined
res.render("index");
});
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 19
Did you ever get to the bottom of this? I’m having the same issue. I’m solely using the default namespace.
Seems to work without
passport.socketio. To be honest, after having worked through this I don’t think I needpassport.socketioat all. Withexpress-socket.io-sessionI guess I can see if the user is authenticated by checking forsocket.handshake.session.passport.userand even check that their JWT expiry at any time.