Skip to content

Instantly share code, notes, and snippets.

@3rd-Eden
Created October 31, 2012 13:15
Show Gist options
  • Save 3rd-Eden/3986964 to your computer and use it in GitHub Desktop.
Save 3rd-Eden/3986964 to your computer and use it in GitHub Desktop.
Socket.IO / Express / Connection sid parsing
// Setup authorization for socket.io to ensure that the user actually has
// access to this socket.io instance
io.set('authorization', function authorization(handshake, done) {
if (!handshake.headers.cookie) return done('No cookie transmitted', false);
var sessionID = /connect.sid\=([^;]+)/g.exec(handshake.headers.cookie);
if (sessionID && sessionID.length) handshake.sessionID = unescape(sessionID[1]).split('.')[0].slice(2);
// Only allow connections that are logged in.
sessions.get(handshake.sessionID, function (err, sess) {
done(err, sess && sess.loggedIn, handshake);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment