-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession.io.js
26 lines (22 loc) · 904 Bytes
/
session.io.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module.exports = function(cookieParser, sessionStore, key, fn){
fn = (typeof key === 'string')? fn: key;
key = (typeof key === 'string')? key: 'connect.sid';
return function(handshake, done){
function next(err, bool){
fn? fn(handshake, done): done(err, bool);
}
if(!handshake || !handshake.headers || !handshake.headers.cookie) next('No cookie in header', false);
cookieParser(handshake, {}, function(err){
if(err) next(err, false);
var session_id = (handshake.secureCookies && handshake.secureCookies[key])
|| (handshake.signedCookies && handshake.signedCookies[key])
|| (handshake.cookies && handshake.cookies[key]);
if(!session_id) next('Could not find cookie with key: ' + key, false);
sessionStore.load(session_id, function(err, session){
if(!session || err) next(err, false);
handshake.session = session;
next(null, true);
});
});
}
}