Authored by htoooth

add

... ... @@ -90,12 +90,12 @@ app.use(compression());
app.use(MemcachedSession({ // eslint-disable-line
proxy: true,
resave: false,
saveUninitialized: true,
saveUninitialized: false,
unset: 'destroy',
secret: '82dd7e724f2c6870472c89dfa43cf48d',
name: 'yohobuy_session',
cookie: {
domain: 'yohobuy.com',
domain: config.cookieDomain,
httpOnly: false
},
store: new MemcachedStore({
... ... @@ -117,36 +117,7 @@ app.use(CookieSession({ // eslint-disable-line
}
}));
app.use((req, res, next) => {
if (req.session) {
let sessionKeys = Object.keys(req.session || {});
let backSessionKeys = Object.keys(req.session2.sessionBack || {});
if (backSessionKeys.length > sessionKeys.length) {
let differences = _.difference(backSessionKeys, sessionKeys);
_.forEach(differences, d => {
req.session[d] = req.session2.sessionBack[d];
});
}
req.session2.sessionBack = req.session;
} else {
req.session = new MemcachedSession.Session(req);
req.session.cookie = new MemcachedSession.Cookie({
domain: 'yohobuy.com',
httpOnly: false
});
req.session = _.assign(req.session, req.session2.sessionBack);
}
if (typeof req.session.reset !== 'function') {
req.session.reset = function() {
req.session = null;
req.session2.reset();
};
}
next();
});
app.use(require('./doraemon/middleware/yohoSession')('session2'));
app.use((req, res, next) => {
req.user = {}; // 全局的用户数据
... ...
... ... @@ -223,13 +223,35 @@ const local = {
res.clearCookie('_UID', {
domain: config.cookieDomain
});
res.clearCookie('TOKEN_', {
res.clearCookie('_TOKEN', {
domain: config.cookieDomain
});
res.clearCookie('_SPK', {
domain: config.cookieDomain
});
res.clearCookie('_g', {
domain: config.cookieDomain
});
res.clearCookie('isRemember', {
domain: config.cookieDomain
});
res.clearCookie('_SPK');
res.clearCookie('_g');
res.clearCookie('isRemember');
res.clearCookie('remem');
res.clearCookie('remem', {
domain: config.cookieDomain
});
res.clearCookie('_SESSION_KEY', {
domain: config.cookieDomain
});
res.clearCookie('isStudent', {
domain: config.cookieDomain
});
let refer = req.get('Referer') || config.siteUrl;
return res.redirect(refer);
... ...
'use strict';
const Fn = require('lodash/fp');
const cookie = global.yoho.cookie;
const authcode = require(`${global.utils}/authcode`);
const Promise = require('bluebird');
const co = Promise.coroutine;
const config = require('../../config/common');
const cache = global.yoho.cache;
const logger = global.yoho.logger;
const crypto = global.yoho.crypto;
// const loginService = require('../../apps/passport/models/login-service');
// const _ = require('lodash');
// const cache = global.yoho.cache;
function decrypt(word) {
return authcode(word, '_SESSION_KEY', 0, 'decode');
}
... ... @@ -25,38 +14,25 @@ module.exports = () => {
// 获得原始请求 url
req.fullUrl = () => req.protocol + '://' + req.get('host') + req.originalUrl;
co(function *() {
// memcached中session的key
let cKey = `${config.sessionMemcachedPrefix}${req.sessionID}`,
uid = 0;
yield cache.get(cKey).catch(err => {
logger.error(`product query save cache data fail:${err.toString()}`);
uid = cookie.getUid(req);
}).then((session) => {
if (session) {
uid = JSON.parse(session).LOGIN_UID_;
}
});
// 个性化推荐id
if (req.cookies._PRID) {
req.user.prid = parseInt(`0${crypto.decrypt('', req.cookies._PRID)}`, 10);
}
// 从 cookie 读取 UID
// 始终从 session 中读取 uid
let uid = req.session.LOGIN_UID_;
if (uid && req.cookies._UID && req.cookies._SESSION_KEY) {
let uidCookie = req.cookies._UID.split('::');
let userInfo = req.cookies._UID.split('::');
let getName = Fn.nth(0);
let getVip = Fn.nth(2);
let getToken = Fn.nth(3);
req.user.name = getName(uidCookie); // 0
req.user.vip = getVip(uidCookie); // 2
req.user.token = getToken(uidCookie); // 3
req.user.name = getName(userInfo); // 0
req.user.vip = getVip(userInfo); // 2
req.user.token = getToken(userInfo); // 3
req.user.isStudent = req.cookies.isStudent || 0;
req.user.uid = {
... ... @@ -71,8 +47,8 @@ module.exports = () => {
};
}
next();
})();
// 记住我
... ...