Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get sessionId from a request?

I use express to support my session management:

app.use(express.cookieParser('your secret here'));
app.use(express.session({secret: 'mysecretcode'}));

I what to extract and save the sessionId from the request. However, the id I get from req.sessionID is different to that sent in the cookie:

req.sessionID --> 'E7oSoKmQfcMKnk5_jA5tF5vR'
cookie.connect.sid --> 's%3AE7oSoKmQfcMKnk5_jA5tF5vR.DQnYdDDcFn8K2JJHMgWL5DTzNYYwIU3DA5a10WImA7U';
  1. Why these two are different?
  2. How can I get connect.sid before sending a response?
like image 972
Luke Avatar asked Oct 21 '22 08:10

Luke


1 Answers

I read the sourcecode of connect.session, and got:

key = options.key || 'connect.sid'

and

var val = 's:' + signature.sign(req.sessionID, secret);
val = cookie.serialize(key, val);
debug('set-cookie %s', val);
res.setHeader('Set-Cookie', val);

so, connect will set cookie into response header when response's 'header' event fired, and when you call response.end(), connect will save session data into store.

That's it.

like image 173
Luke Avatar answered Oct 27 '22 09:10

Luke