-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
35 lines (27 loc) · 922 Bytes
/
client.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
27
28
29
30
31
32
33
34
35
// cookie handling
var SID = {
get: function (name) {
// credentials: https://developer.mozilla.org/en-US/docs/Web/API/Document.cookie
return document.cookie.replace(new RegExp('(?:(?:^|.*;)\\s*' + name.replace(/[\-\.\+\*]/g, '\\$&') + '\\s*\\=\\s*([^;]*).*$)|^.*$'), '$1') || null;
},
set: function (name, value) {
document.cookie = name + '=' + value + ';path=/';
},
rm: function (name) {
document.cookie = name + '=;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT';
}
};
// set or remove cookie session
exports.session = function (_options, data, next) {
options = _options || {
sid: this._config.cookie || 'sid'
};
// set session
if (!options.remove && data && data[options.sid]) {
SID.set(options.sid, data[options.sid]);
// remove session
} else {
SID.rm(options.sid);
}
next(null, data)
};