@@ -81,27 +81,30 @@ export class UserController extends AdaptableController {
81
81
}
82
82
83
83
checkResetTokenValidity ( username , token ) {
84
- let query = {
85
- username : username ,
86
- _perishable_token : token ,
87
- } ;
88
- if ( ! token ) {
89
- query = { $or : [ { email : username } , { username, email : { $exists : false } } ] } ;
90
- }
91
- return this . config . database . find ( '_User' , query , { limit : 1 } ) . then ( results => {
92
- if ( results . length != 1 ) {
93
- throw 'Failed to reset password: username / email / token is invalid' ;
94
- }
84
+ return this . config . database
85
+ . find (
86
+ '_User' ,
87
+ {
88
+ username : username ,
89
+ _perishable_token : token ,
90
+ } ,
91
+ { limit : 1 }
92
+ )
93
+ . then ( results => {
94
+ if ( results . length != 1 ) {
95
+ throw 'Failed to reset password: username / email / token is invalid' ;
96
+ }
95
97
96
- if ( this . config . passwordPolicy && this . config . passwordPolicy . resetTokenValidityDuration ) {
97
- let expiresDate = results [ 0 ] . _perishable_token_expires_at ;
98
- if ( expiresDate && expiresDate . __type == 'Date' ) {
99
- expiresDate = new Date ( expiresDate . iso ) ;
98
+ if ( this . config . passwordPolicy && this . config . passwordPolicy . resetTokenValidityDuration ) {
99
+ let expiresDate = results [ 0 ] . _perishable_token_expires_at ;
100
+ if ( expiresDate && expiresDate . __type == 'Date' ) {
101
+ expiresDate = new Date ( expiresDate . iso ) ;
102
+ }
103
+ if ( expiresDate < new Date ( ) ) throw 'The password reset link has expired' ;
100
104
}
101
- if ( expiresDate < new Date ( ) ) throw 'The password reset link has expired' ;
102
- }
103
- return results [ 0 ] ;
104
- } ) ;
105
+
106
+ return results [ 0 ] ;
107
+ } ) ;
105
108
}
106
109
107
110
getUserIfNeeded ( user ) {
@@ -208,10 +211,24 @@ export class UserController extends AdaptableController {
208
211
this . config . passwordPolicy . resetTokenReuseIfValid &&
209
212
this . config . passwordPolicy . resetTokenValidityDuration
210
213
) {
211
- try {
212
- user = await this . checkResetTokenValidity ( email ) ;
213
- } catch ( e ) {
214
- /* */
214
+ const results = await this . config . database . find (
215
+ '_User' ,
216
+ {
217
+ $or : [
218
+ { email, _perishable_token : { $exists : true } } ,
219
+ { username : email , email : { $exists : false } , _perishable_token : { $exists : true } } ,
220
+ ] ,
221
+ } ,
222
+ { limit : 1 }
223
+ ) ;
224
+ if ( results . length == 1 ) {
225
+ let expiresDate = results [ 0 ] . _perishable_token_expires_at ;
226
+ if ( expiresDate && expiresDate . __type == 'Date' ) {
227
+ expiresDate = new Date ( expiresDate . iso ) ;
228
+ }
229
+ if ( expiresDate > new Date ( ) ) {
230
+ user = results [ 0 ] ;
231
+ }
215
232
}
216
233
}
217
234
if ( ! user || ! user . _perishable_token ) {
0 commit comments