user.js
1.27 KB
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
'use strict';
const _ = require('lodash');
const cookie = global.yoho.cookie;
const crypto = global.yoho.crypto;
module.exports = () => {
return (req, res, next) => {
// 从 SESSION 中获取到当前登录用户的 UID
// if (req.session && _.isNumber(req.session.LOGIN_UID)) {
// // 不要使用 === 判断uid的值,如果需要判断使用 ==
// req.user.uid = {
// toString: () => {
// return req.session.LOGIN_UID;
// },
// sessionKey: req.session.SESSION_KEY
// };
// let userData = _.get(req.session, 'USER', {});
// _.merge(req.user, userData);
// }
// session 没有读取到的时候,从 cookie 读取 UID
if (!req.user.uid && req.cookies._UID) {
// 不要使用 === 判断uid的值,如果需要判断使用 ==
let sessionKey = req.cookies._SESSION_KEY && crypto.decrypt('yoho9646abcdefgh', req.cookies._SESSION_KEY);
sessionKey = sessionKey.replace(/[^\w:-]/g, '');
req.user.uid = {
toString: () => {
return cookie.getUid(req);
},
sessionKey
};
}
next();
};
};