Authored by ccbikai

完善 UID 获取

Showing 1 changed file with 13 additions and 0 deletions
@@ -22,6 +22,8 @@ const memcached = require('yoho-connect-memcached'); @@ -22,6 +22,8 @@ const memcached = require('yoho-connect-memcached');
22 const uuid = require('uuid'); 22 const uuid = require('uuid');
23 const _ = require('lodash'); 23 const _ = require('lodash');
24 const pkg = require('./package.json'); 24 const pkg = require('./package.json');
  25 +const sign = require('./library/sign');
  26 +const cookie = require('./library/cookie');
25 27
26 const app = express(); 28 const app = express();
27 const MemcachedStore = memcached(session); 29 const MemcachedStore = memcached(session);
@@ -70,6 +72,17 @@ app.use((req, res, next) => { @@ -70,6 +72,17 @@ app.use((req, res, next) => {
70 if (req.session && _.isNumber(req.session._LOGIN_UID)) { 72 if (req.session && _.isNumber(req.session._LOGIN_UID)) {
71 req.user.uid = req.session._LOGIN_UID; 73 req.user.uid = req.session._LOGIN_UID;
72 } 74 }
  75 +
  76 + // session 没有读取到的时候,从 cookie 读取 UID
  77 + if (!req.user.uid && req.cookies._UID) {
  78 + let uid = cookie.getUid(req);
  79 +
  80 + // 校验 cookie 的 uid 有没有被修改
  81 + if (req.cookies._TOKEN === sign.makeToken(uid)) {
  82 + req.user.uid = uid;
  83 + }
  84 + }
  85 +
73 next(); 86 next();
74 }); 87 });
75 88