Authored by 姜枫

fix session key

@@ -26,6 +26,16 @@ const cache = global.yoho.cache; @@ -26,6 +26,16 @@ const cache = global.yoho.cache;
26 let siteUrl = config.siteUrl.indexOf('//') === 0 ? 'http:' + config.siteUrl : config.siteUrl; 26 let siteUrl = config.siteUrl.indexOf('//') === 0 ? 'http:' + config.siteUrl : config.siteUrl;
27 27
28 28
  29 +function getLoginStat(account) {
  30 + let errorLoginKey = 'account_errorlogin_' + account;
  31 + let accountKey = 'account_signin_' + account;
  32 + let cacheGet = [cache.get(errorLoginKey), cache.get(accountKey)];
  33 +
  34 + return Promise.all(cacheGet).catch(() => {
  35 + return [0, 0];
  36 + });
  37 +}
  38 +
29 // 本地登录 39 // 本地登录
30 passport.use('local', new LocalStrategy({ 40 passport.use('local', new LocalStrategy({
31 usernameField: 'account', 41 usernameField: 'account',
@@ -54,9 +64,7 @@ passport.use('local', new LocalStrategy({ @@ -54,9 +64,7 @@ passport.use('local', new LocalStrategy({
54 let errorLoginKey = 'account_errorlogin_' + account; 64 let errorLoginKey = 'account_errorlogin_' + account;
55 let accountKey = 'account_signin_' + account; 65 let accountKey = 'account_signin_' + account;
56 66
57 - let cacheGet = [cache.get(errorLoginKey), cache.get(accountKey)];  
58 -  
59 - Promise.all(cacheGet).then(times => { 67 + getLoginStat(account).then(times => {
60 let errLoginTimes = _.parseInt(times[0]) || 0; 68 let errLoginTimes = _.parseInt(times[0]) || 0;
61 let accountTimes = _.parseInt(times[1]) || 0; 69 let accountTimes = _.parseInt(times[1]) || 0;
62 70
@@ -65,14 +73,14 @@ passport.use('local', new LocalStrategy({ @@ -65,14 +73,14 @@ passport.use('local', new LocalStrategy({
65 } else { 73 } else {
66 return LoginApi.signin(type, area, username, password, shoppingKey).then((result) => { 74 return LoginApi.signin(type, area, username, password, shoppingKey).then((result) => {
67 if (result.code && result.code === 200 && result.data.uid) { 75 if (result.code && result.code === 200 && result.data.uid) {
68 - cache.del(errorLoginKey); 76 + cache.del(errorLoginKey).catch(() => {});
69 77
70 done(null, result.data); 78 done(null, result.data);
71 } else { 79 } else {
72 errLoginTimes = errLoginTimes + 1; 80 errLoginTimes = errLoginTimes + 1;
73 accountTimes = accountTimes + 1; 81 accountTimes = accountTimes + 1;
74 - cache.set(errorLoginKey, errLoginTimes);  
75 - cache.set(accountKey, accountTimes, 1800); 82 + cache.set(errorLoginKey, errLoginTimes).catch(() => {});
  83 + cache.set(accountKey, accountTimes, 1800).catch(() => {});
76 84
77 // 再次校验 85 // 再次校验
78 if (accountTimes >= 10) { 86 if (accountTimes >= 10) {
@@ -42,6 +42,7 @@ function doPassportCallback(req, res, user) { @@ -42,6 +42,7 @@ function doPassportCallback(req, res, user) {
42 42
43 return loginService.signinByOpenID(user.nickname, user.openId, user.sourceType, shoppingKey) 43 return loginService.signinByOpenID(user.nickname, user.openId, user.sourceType, shoppingKey)
44 .then((result) => { 44 .then((result) => {
  45 + console.log(result);
45 if (result.code !== 200) { 46 if (result.code !== 200) {
46 return res.redirect(config.siteUrl); 47 return res.redirect(config.siteUrl);
47 } 48 }
@@ -52,7 +53,7 @@ function doPassportCallback(req, res, user) { @@ -52,7 +53,7 @@ function doPassportCallback(req, res, user) {
52 refer: refer 53 refer: refer
53 }); 54 });
54 } else if (result.data['is_bind'] === 'Y' && result.data.uid) {//eslint-disable-line 55 } else if (result.data['is_bind'] === 'Y' && result.data.uid) {//eslint-disable-line
55 - return loginService.syncUserSession(result.data.uid, req, res).then(() => { 56 + return loginService.syncUserSession(result.data.uid, result.data.session_key, req, res).then(() => {
56 return refer; 57 return refer;
57 }); 58 });
58 } 59 }
@@ -177,7 +178,7 @@ const local = { @@ -177,7 +178,7 @@ const local = {
177 } 178 }
178 }()); 179 }());
179 180
180 - loginService.syncUserSession(user.uid, req, res).then(() => { 181 + loginService.syncUserSession(user.uid, user.session_key, req, res).then(() => {
181 return res.json({ 182 return res.json({
182 code: 200, 183 code: 200,
183 data: { 184 data: {
@@ -24,7 +24,11 @@ const signin = (type, area, profile, password, shoppingKey) => { @@ -24,7 +24,11 @@ const signin = (type, area, profile, password, shoppingKey) => {
24 return loginBy[type](area, profile, password, shoppingKey); 24 return loginBy[type](area, profile, password, shoppingKey);
25 }; 25 };
26 26
27 -const syncUserSession = (uid, req, res) => { 27 +const syncUserSession = (uid, sessionKey, req, res) => {
  28 + cache.set(`java_session_key:${uid}`, sessionKey).catch(() => {
  29 + global.yoho.logger.error('write session key fail');
  30 + });
  31 +
28 return Promise.all([userService.profile(uid), cartService.goodsCount(uid)]).spread((userInfo, count) => { 32 return Promise.all([userService.profile(uid), cartService.goodsCount(uid)]).spread((userInfo, count) => {
29 let token = sign.makeToken(uid); 33 let token = sign.makeToken(uid);
30 let data = userInfo.data; 34 let data = userInfo.data;
@@ -75,9 +79,9 @@ const rememberAccount = (accountInfo, req, res) => { @@ -75,9 +79,9 @@ const rememberAccount = (accountInfo, req, res) => {
75 domain: config.cookieDomain 79 domain: config.cookieDomain
76 }); 80 });
77 81
78 - if (!cache.get(rememKey)) {  
79 - cache.set(rememKey, accountInfo, aWeek);  
80 - } 82 + // if (!cache.get(rememKey)) {
  83 + // cache.set(rememKey, accountInfo, aWeek);
  84 + // }
81 }; 85 };
82 86
83 module.exports = { 87 module.exports = {
@@ -5,7 +5,7 @@ const SECOND = 1; @@ -5,7 +5,7 @@ const SECOND = 1;
5 const MINUTE = 60 * SECOND; 5 const MINUTE = 60 * SECOND;
6 6
7 const cachePage = { 7 const cachePage = {
8 - '/': 5 * MINUTE, 8 + '/': 30 * SECOND,
9 9
10 // 频道页 10 // 频道页
11 '/boys': 30 * SECOND, 11 '/boys': 30 * SECOND,
@@ -29,26 +29,28 @@ module.exports = () => { @@ -29,26 +29,28 @@ module.exports = () => {
29 } 29 }
30 30
31 // 记住我 31 // 记住我
32 - if (_.isEmpty(req.user) && req.cookies.isRemember === 'true' && req.cookies.remem) {  
33 - return cache.get(req.cookies.remem).then((result) => {  
34 - let data = JSON.parse(result || '{}');  
35 -  
36 - let area = data.area;  
37 - let account = data.account;  
38 - let password = data.password;  
39 -  
40 - return loginService.signin('password', area, account, password);  
41 - }).then((result) => {  
42 - if (result.code !== 200) {  
43 - return Promise.reject();  
44 - }  
45 -  
46 - return loginService.syncUserSession(result.data.uid, req, res);  
47 - }).then(() => {  
48 - return res.redirect(req.fullUrl());  
49 - }).catch(next);  
50 - } else {  
51 - return next();  
52 - } 32 + // if (_.isEmpty(req.user) && req.cookies.isRemember === 'true' && req.cookies.remem) {
  33 + // return cache.get(req.cookies.remem).then((result) => {
  34 + // let data = JSON.parse(result || '{}');
  35 +
  36 + // let area = data.area;
  37 + // let account = data.account;
  38 + // let password = data.password;
  39 +
  40 + // return loginService.signin('password', area, account, password);
  41 + // }).then((result) => {
  42 + // if (result.code !== 200) {
  43 + // return Promise.reject();
  44 + // }
  45 +
  46 + // return loginService.syncUserSession(result.data.uid, req, res);
  47 + // }).then(() => {
  48 + // return res.redirect(req.fullUrl());
  49 + // }).catch(next);
  50 + // } else {
  51 + // return next();
  52 + // }
  53 +
  54 + next();
53 }; 55 };
54 }; 56 };