Authored by 姜枫

fix session key

... ... @@ -26,6 +26,16 @@ const cache = global.yoho.cache;
let siteUrl = config.siteUrl.indexOf('//') === 0 ? 'http:' + config.siteUrl : config.siteUrl;
function getLoginStat(account) {
let errorLoginKey = 'account_errorlogin_' + account;
let accountKey = 'account_signin_' + account;
let cacheGet = [cache.get(errorLoginKey), cache.get(accountKey)];
return Promise.all(cacheGet).catch(() => {
return [0, 0];
});
}
// 本地登录
passport.use('local', new LocalStrategy({
usernameField: 'account',
... ... @@ -54,9 +64,7 @@ passport.use('local', new LocalStrategy({
let errorLoginKey = 'account_errorlogin_' + account;
let accountKey = 'account_signin_' + account;
let cacheGet = [cache.get(errorLoginKey), cache.get(accountKey)];
Promise.all(cacheGet).then(times => {
getLoginStat(account).then(times => {
let errLoginTimes = _.parseInt(times[0]) || 0;
let accountTimes = _.parseInt(times[1]) || 0;
... ... @@ -65,14 +73,14 @@ passport.use('local', new LocalStrategy({
} else {
return LoginApi.signin(type, area, username, password, shoppingKey).then((result) => {
if (result.code && result.code === 200 && result.data.uid) {
cache.del(errorLoginKey);
cache.del(errorLoginKey).catch(() => {});
done(null, result.data);
} else {
errLoginTimes = errLoginTimes + 1;
accountTimes = accountTimes + 1;
cache.set(errorLoginKey, errLoginTimes);
cache.set(accountKey, accountTimes, 1800);
cache.set(errorLoginKey, errLoginTimes).catch(() => {});
cache.set(accountKey, accountTimes, 1800).catch(() => {});
// 再次校验
if (accountTimes >= 10) {
... ...
... ... @@ -42,6 +42,7 @@ function doPassportCallback(req, res, user) {
return loginService.signinByOpenID(user.nickname, user.openId, user.sourceType, shoppingKey)
.then((result) => {
console.log(result);
if (result.code !== 200) {
return res.redirect(config.siteUrl);
}
... ... @@ -52,7 +53,7 @@ function doPassportCallback(req, res, user) {
refer: refer
});
} else if (result.data['is_bind'] === 'Y' && result.data.uid) {//eslint-disable-line
return loginService.syncUserSession(result.data.uid, req, res).then(() => {
return loginService.syncUserSession(result.data.uid, result.data.session_key, req, res).then(() => {
return refer;
});
}
... ... @@ -177,7 +178,7 @@ const local = {
}
}());
loginService.syncUserSession(user.uid, req, res).then(() => {
loginService.syncUserSession(user.uid, user.session_key, req, res).then(() => {
return res.json({
code: 200,
data: {
... ...
... ... @@ -24,7 +24,11 @@ const signin = (type, area, profile, password, shoppingKey) => {
return loginBy[type](area, profile, password, shoppingKey);
};
const syncUserSession = (uid, req, res) => {
const syncUserSession = (uid, sessionKey, req, res) => {
cache.set(`java_session_key:${uid}`, sessionKey).catch(() => {
global.yoho.logger.error('write session key fail');
});
return Promise.all([userService.profile(uid), cartService.goodsCount(uid)]).spread((userInfo, count) => {
let token = sign.makeToken(uid);
let data = userInfo.data;
... ... @@ -75,9 +79,9 @@ const rememberAccount = (accountInfo, req, res) => {
domain: config.cookieDomain
});
if (!cache.get(rememKey)) {
cache.set(rememKey, accountInfo, aWeek);
}
// if (!cache.get(rememKey)) {
// cache.set(rememKey, accountInfo, aWeek);
// }
};
module.exports = {
... ...
... ... @@ -5,7 +5,7 @@ const SECOND = 1;
const MINUTE = 60 * SECOND;
const cachePage = {
'/': 5 * MINUTE,
'/': 30 * SECOND,
// 频道页
'/boys': 30 * SECOND,
... ...
... ... @@ -29,26 +29,28 @@ module.exports = () => {
}
// 记住我
if (_.isEmpty(req.user) && req.cookies.isRemember === 'true' && req.cookies.remem) {
return cache.get(req.cookies.remem).then((result) => {
let data = JSON.parse(result || '{}');
let area = data.area;
let account = data.account;
let password = data.password;
return loginService.signin('password', area, account, password);
}).then((result) => {
if (result.code !== 200) {
return Promise.reject();
}
return loginService.syncUserSession(result.data.uid, req, res);
}).then(() => {
return res.redirect(req.fullUrl());
}).catch(next);
} else {
return next();
}
// if (_.isEmpty(req.user) && req.cookies.isRemember === 'true' && req.cookies.remem) {
// return cache.get(req.cookies.remem).then((result) => {
// let data = JSON.parse(result || '{}');
// let area = data.area;
// let account = data.account;
// let password = data.password;
// return loginService.signin('password', area, account, password);
// }).then((result) => {
// if (result.code !== 200) {
// return Promise.reject();
// }
// return loginService.syncUserSession(result.data.uid, req, res);
// }).then(() => {
// return res.redirect(req.fullUrl());
// }).catch(next);
// } else {
// return next();
// }
next();
};
};
... ...