-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddlewares.js
48 lines (39 loc) · 1.3 KB
/
middlewares.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
36
37
38
39
40
41
42
43
44
45
46
47
48
var expressEndFunc;
module.exports = {
endFunctionSaver: function (req, res, next) {
if (!expressEndFunc) {
expressEndFunc = res.end;
}
next();
},
sessionEndFunctionHandler: function (req, res, next) {
var sessionEndFunc = res.end;
res.end = function end(chunk, encoding) {
try {
sessionEndFunc.call(res, chunk, encoding);
} catch(e) {
expressEndFunc.call(res, chunk, encoding);
}
};
next();
},
adminHandler: function (req, res, next) {
if (req.headers && req.headers['verified'] == 'SUCCESS' && req.session) {
req.session.isAdmin = true;
} else {
if (req.session && req.session.isAdmin) {
delete req.session.isAdmin;
}
}
next();
},
cookieSettingsHandler: function (req, res, next) {
if (req.cookies && req.cookies['settings']) {
var settingsCookieValue = req.cookies['settings'];
var settingKey = settingsCookieValue.slice(0, settingsCookieValue.indexOf(':'));
var settingValue = settingsCookieValue.slice(settingsCookieValue.indexOf(':') + 1)[0];
req.session[settingKey] = settingValue;
}
next();
}
}