@@ -7,6 +7,7 @@ var security = require('../utils/security');
77var factory = require ( '../utils/factory' ) ;
88var moment = require ( 'moment' ) ;
99var EmailManager = require ( './email-manager' ) ;
10+ var config = require ( '../config' ) ;
1011
1112var proto = module . exports = function ( ) {
1213 function AccountManager ( ) {
@@ -103,6 +104,7 @@ proto.login = function (account, password) {
103104 } else {
104105 where = { username : account } ;
105106 }
107+ var tryLoginTimes = _ . get ( config , 'common.tryLoginTimes' , 0 ) ;
106108 return models . Users . findOne ( { where : where } )
107109 . then ( function ( users ) {
108110 if ( _ . isEmpty ( users ) ) {
@@ -111,30 +113,37 @@ proto.login = function (account, password) {
111113 return users ;
112114 } )
113115 . then ( function ( users ) {
114- var loginKey = `${ LOGIN_LIMIT_PRE } ${ users . id } ` ;
115- return factory . getRedisClient ( "default" ) . getAsync ( loginKey )
116- . then ( function ( loginErrorTimes ) {
117- if ( loginErrorTimes > 10 ) {
118- throw new Error ( `您输入密码错误次数超过限制,帐户已经锁定` ) ;
119- }
116+ if ( tryLoginTimes > 0 ) {
117+ var loginKey = `${ LOGIN_LIMIT_PRE } ${ users . id } ` ;
118+ var client = factory . getRedisClient ( "default" ) ;
119+ return client . getAsync ( loginKey )
120+ . then ( function ( loginErrorTimes ) {
121+ if ( loginErrorTimes > tryLoginTimes ) {
122+ throw new Error ( `您输入密码错误次数超过限制,帐户已经锁定` ) ;
123+ }
124+ return users ;
125+ } ) ;
126+ } else {
120127 return users ;
121- } ) ;
128+ }
122129 } )
123130 . then ( function ( users ) {
124131 if ( ! security . passwordVerifySync ( password , users . password ) ) {
125- var loginKey = `${ LOGIN_LIMIT_PRE } ${ users . id } ` ;
126- var client = factory . getRedisClient ( "default" ) ;
127- client . existsAsync ( loginKey )
128- . then ( function ( isExists ) {
129- if ( ! isExists ) {
130- var expires = moment ( ) . endOf ( 'day' ) . format ( 'X' ) - moment ( ) . format ( 'X' ) ;
131- return client . setexAsync ( loginKey , expires , 0 ) ;
132- }
133- return isExists ;
134- } )
135- . then ( function ( ) {
136- return client . incrAsync ( loginKey ) ;
137- } )
132+ if ( tryLoginTimes > 0 ) {
133+ var loginKey = `${ LOGIN_LIMIT_PRE } ${ users . id } ` ;
134+ var client = factory . getRedisClient ( "default" ) ;
135+ client . existsAsync ( loginKey )
136+ . then ( function ( isExists ) {
137+ if ( ! isExists ) {
138+ var expires = moment ( ) . endOf ( 'day' ) . format ( 'X' ) - moment ( ) . format ( 'X' ) ;
139+ return client . setexAsync ( loginKey , expires , 0 ) ;
140+ }
141+ return isExists ;
142+ } )
143+ . then ( function ( ) {
144+ return client . incrAsync ( loginKey ) ;
145+ } ) ;
146+ }
138147 throw new Error ( "account or password error." ) ;
139148 } else {
140149 return users ;
0 commit comments