Sharing Express and socket.io sessions.

1,205 views
Skip to first unread message

Thijs Koerselman

unread,
Aug 24, 2012, 10:47:21 AM8/24/12
to nod...@googlegroups.com
I'm trying to use express with socket.io and make them share sessions. I have found examples online and in my "Node up and running" book but somehow I can't get it to work. I must be doing something stupid.

The problem is that in the authorization function of the socket sessionStore.get() always seems to return undefined for both error and session object. Here's my authorize function:

sio.configure(function(){
  sio.set('authorization', function(data, accept){

    if(data.headers.cookie){
      // parseCookie not available anymore
      // var cookies = utils.parseCookie(data.headers.cookie);
      var cookies = cookie.parse(data.headers.cookie);

      data.sessionID = cookies['express.sid'];
      console.log("sessionID", data.sessionID);

      data.sessionStore = sessionStore;
      console.log("store", util.inspect(sessionStore, true, null));

      sessionStore.get(data.sessionID, function(err, session) {
        if( err || !session) {
          console.log("ERROR", err);
          console.log("session:", util.inspect(session));
          return accept("Invalid session", false);
        }
        console.log("creating session data");
        data.session = new Session(data, session);

        accept(null, true);
      });
    }else{
       return accept("No cookie transmitted.", false);
    }
  });
});

And this is the output:

sessionID s:fTZNjPuFIIr5sZvXLCwfR/EQ.6r41SUae3OAQokT58uypmCnajApCyFEfDaWakmlTfYY
store { generate: 
   { [Function]
     [prototype]: { [constructor]: [Circular] },
     [name]: '',
     [arguments]: null,
     [length]: 1,
     [caller]: null },
  sessions: { 'fTZNjPuFIIr5sZvXLCwfR/EQ': '{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}' } }
ERROR undefined
session: undefined
   debug - authorized
   warn  - handshake error Invalid session


As I see it, the session store clearly holds a session with an id similar to the session id (only up until the "."?), so I don't understand why the get() function returns nothing. Where is that function defined anyway and why doesn't it show up in the util.inspect printout?

This is what I'm using:

├─┬ exp...@3.0.0rc3
│ ├── comm...@0.6.1
│ ├─┬ con...@2.4.3
│ │ ├── by...@0.1.0
│ │ ├── formi...@1.0.11
│ │ ├── pa...@0.0.1
│ │ └── q...@0.4.2
│ ├── coo...@0.0.4
│ ├── c...@0.2.0
│ ├── de...@0.7.0
│ ├── fr...@0.1.0
│ ├── met...@0.0.1
│ ├── mkd...@0.3.3
│ ├── range-...@0.0.4
│ └─┬ se...@0.0.3
│   └── mi...@1.2.6

TZ (天猪)

unread,
Aug 25, 2012, 11:10:07 AM8/25/12
to nod...@googlegroups.com
a simple example:  https://gist.github.com/3337459

在 2012年8月24日星期五UTC+8下午10时47分21秒,0x80写道:

Thijs Koerselman

unread,
Aug 25, 2012, 11:45:25 AM8/25/12
to nod...@googlegroups.com
Wonderful! Thank you so much. It's all starting to make sense now.

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply all
Reply to author
Forward
0 new messages