I have encountered two possibilities and would prefer a solution that performs the check prior to fully establishing the websocket.
var express = require("express.io");
var app = express().http().io();
app.use(express.json());
app.use(express.cookieParser());
app.use(express.session({secret: process.env.COOKIESECRET}));
Option 1: How to get the Express session object?
UPDATE: This might not be workable as Express.io registers its own "authorize" function which makes the Express session available to Socket.io.
app.io.configure(function() {
app.io.set("authorize", function(handshake, authorize) {
// Cookie is available...?
//handshake.headers.cookie
});
});
Option 2: Easy to get Express session, but connection already established.
app.io.route("test", function(req) {
if(!req.session.IsAuthorized) {
req.io.disconnect();
}
});