|
|
const _ = require('lodash');
|
|
|
const cookie = global.yoho.cookie;
|
|
|
const md5 = require('yoho-md5');
|
|
|
|
|
|
module.exports = () => {
|
|
|
return (req, res, next) => {
|
|
|
// 从 SESSION 中获取到当前登录用户的 UID
|
|
|
if (req.session && _.isNumber(req.session._LOGIN_UID)) {
|
|
|
req.user.uid = req.session._LOGIN_UID;
|
|
|
}
|
|
|
const uid = req.cookies._YOHOUID;
|
|
|
const token = req.cookies._YOHOTOKEN;
|
|
|
const key = `-_-!!!${uid}!!!-_-`;
|
|
|
|
|
|
// session 没有读取到的时候,从 cookie 读取 UID
|
|
|
if (!req.user.uid && req.cookies._UID) {
|
|
|
req.user.uid = cookie.getUid(req);
|
|
|
if (md5(key) === token) {
|
|
|
req.user.uid = uid;
|
|
|
}
|
|
|
|
|
|
next();
|
...
|
...
|
|