Blame view

doraemon/middleware/user.js 627 Bytes
陈轩 authored
1 2
'use strict';
ccbikai authored
3
const _ = require('lodash');
姜枫 authored
4
const cookie = global.yoho.cookie;
ccbikai authored
5 6 7 8

module.exports = () => {
    return (req, res, next) => {
        // 从 SESSION 中获取到当前登录用户的 UID
9 10
        if (req.session && _.isNumber(req.session.LOGIN_UID)) {
            req.user.uid = req.session.LOGIN_UID;
陈轩 authored
11 12 13 14

            let userData = _.get(req.session, 'USER', {});

            _.merge(req.user, userData);
ccbikai authored
15 16 17 18 19 20 21 22 23 24
        }

        // session 没有读取到的时候,从 cookie 读取 UID
        if (!req.user.uid && req.cookies._UID) {
            req.user.uid = cookie.getUid(req);
        }

        next();
    };
};