Skip to content

Instantly share code, notes, and snippets.

@teddyknox
Created June 23, 2013 23:02
Show Gist options
  • Save teddyknox/5846852 to your computer and use it in GitHub Desktop.
Save teddyknox/5846852 to your computer and use it in GitHub Desktop.
Socket.io Express 3.0 session retrieval function
io.set('authorization', function(handshake, callback) {
if (handshake && handshake.headers && handshake.headers.cookie) {
cookieParser(handshake, {}, function(err) {
if(err) {
return callback('COOKIE_PARSE_ERROR', false);
}
var sessionId = handshake.signedCookies[config.cookie];
sessionStore.load(sessionId, function(err, session) {
if(err || !session) {//|| !session.auth || !session.auth.loggedIn) {
callback('NOT_LOGGED_IN', false);
}
else{
// console.log(Object.keys(session));
handshake.session = session;
callback(null, true);
}
});
});
} else {
return callback('MISSING_COOKIE', false);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment