Showing
1 changed file
with
30 additions
and
46 deletions
@@ -25,15 +25,10 @@ const cache = global.yoho.cache; | @@ -25,15 +25,10 @@ const cache = global.yoho.cache; | ||
25 | 25 | ||
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 | - | ||
29 | function getLoginStat(ip) { | 28 | function getLoginStat(ip) { |
30 | let errorLoginKey = 'loginErrorIp:' + ip; | 29 | let errorLoginKey = 'loginErrorIp:' + ip; |
31 | - let accountKey = 'signinErrorIp:' + ip; | ||
32 | - let cacheGet = [cache.get(errorLoginKey), cache.get(accountKey)]; | ||
33 | 30 | ||
34 | - return Promise.all(cacheGet).catch(() => { | ||
35 | - return [0, 0]; | ||
36 | - }); | 31 | + return cache.get(errorLoginKey); |
37 | } | 32 | } |
38 | 33 | ||
39 | // 本地登录 | 34 | // 本地登录 |
@@ -42,7 +37,6 @@ passport.use('local', new LocalStrategy({ | @@ -42,7 +37,6 @@ passport.use('local', new LocalStrategy({ | ||
42 | passwordField: 'password', | 37 | passwordField: 'password', |
43 | passReqToCallback: true | 38 | passReqToCallback: true |
44 | }, (req, username, password, done) => { | 39 | }, (req, username, password, done) => { |
45 | - | ||
46 | let area = req.body.areaCode || '86'; | 40 | let area = req.body.areaCode || '86'; |
47 | 41 | ||
48 | if (_.isEmpty(password)) { | 42 | if (_.isEmpty(password)) { |
@@ -55,48 +49,38 @@ passport.use('local', new LocalStrategy({ | @@ -55,48 +49,38 @@ passport.use('local', new LocalStrategy({ | ||
55 | 49 | ||
56 | let clientIp = req.yoho.clientIp; | 50 | let clientIp = req.yoho.clientIp; |
57 | let errorLoginKey = 'loginErrorIp:' + clientIp; | 51 | let errorLoginKey = 'loginErrorIp:' + clientIp; |
58 | - let accountKey = 'signinErrorIp:' + clientIp; | ||
59 | - | ||
60 | - getLoginStat(clientIp).then(times => { | ||
61 | - let errLoginTimes = _.parseInt(times[0]) || 0; | ||
62 | - let accountTimes = _.parseInt(times[1]) || 0; | ||
63 | - | ||
64 | - if (accountTimes >= 10) { | ||
65 | - done({message: '您的账号已被暂时锁定,请稍后再试'}, null); | ||
66 | - } else { | ||
67 | - return LoginApi.signin(type, area, username, password, shoppingKey, clientIp).then((result) => { | ||
68 | - if (result.code && result.code === 200 && result.data.uid) { | ||
69 | - cache.del(errorLoginKey).catch(() => {}); | ||
70 | 52 | ||
71 | - req.session.type = ''; | ||
72 | - done(null, result.data); | 53 | + getLoginStat(clientIp).then((times) => { |
54 | + let errLoginTimes = _.parseInt(times) || 0; | ||
55 | + | ||
56 | + return LoginApi.signin(type, area, username, password, shoppingKey, clientIp).then((result) => { | ||
57 | + if (result.code && result.code === 200 && result.data.uid) { | ||
58 | + cache.del(errorLoginKey).catch(() => {}); | ||
59 | + req.session.type = ''; | ||
60 | + | ||
61 | + done(null, result.data); | ||
62 | + } else { | ||
63 | + errLoginTimes = errLoginTimes + 1; | ||
64 | + cache.set(errorLoginKey, errLoginTimes, 3600).catch(() => {}); | ||
65 | + | ||
66 | + // 再次校验 | ||
67 | + if (errLoginTimes >= 1) { | ||
68 | + req.session.type = 'needCaptcha'; | ||
69 | + done({ | ||
70 | + message: `您输入的密码及账户名不匹配, | ||
71 | + 是否<a href="${helpers.urlFormat('/passport/back/index')}" target="_blank">忘记密码?</a>`, | ||
72 | + needCaptcha: true, | ||
73 | + type: type | ||
74 | + }); | ||
73 | } else { | 75 | } else { |
74 | - errLoginTimes = errLoginTimes + 1; | ||
75 | - accountTimes = accountTimes + 1; | ||
76 | - cache.set(errorLoginKey, errLoginTimes).catch(() => {}); | ||
77 | - cache.set(accountKey, accountTimes, 3600).catch(() => {}); | ||
78 | - | ||
79 | - // 再次校验 | ||
80 | - if (accountTimes >= 10) { | ||
81 | - done({message: '您的账号已被暂时锁定,请稍后再试'}, null); | ||
82 | - } else if (errLoginTimes >= 1) { | ||
83 | - req.session.type = 'needCaptcha'; | ||
84 | - done({ | ||
85 | - message: `您输入的密码及账户名不匹配, | ||
86 | - 是否<a href="${helpers.urlFormat('/passport/back/index')}" target="_blank">忘记密码?</a>`, | ||
87 | - needCaptcha: true, | ||
88 | - type: type | ||
89 | - }); | ||
90 | - } else { | ||
91 | - done({ | ||
92 | - message: `您输入的密码及账户名不匹配, | ||
93 | - 是否<a href="${helpers.urlFormat('/passport/back/index')}" target="_blank">忘记密码?</a>`, | ||
94 | - needCaptcha: false | ||
95 | - }); | ||
96 | - } | 76 | + done({ |
77 | + message: `您输入的密码及账户名不匹配, | ||
78 | + 是否<a href="${helpers.urlFormat('/passport/back/index')}" target="_blank">忘记密码?</a>`, | ||
79 | + needCaptcha: false | ||
80 | + }); | ||
97 | } | 81 | } |
98 | - }); | ||
99 | - } | 82 | + } |
83 | + }); | ||
100 | }).catch(e => { | 84 | }).catch(e => { |
101 | logger.error('call the signin service fail,', e); | 85 | logger.error('call the signin service fail,', e); |
102 | done('登录失败,请稍后重试', null); | 86 | done('登录失败,请稍后重试', null); |
-
Please register or login to post a comment