Skip to content

Commit 4a6eb57

Browse files
committed
Update UserController.js
1 parent 9aa489a commit 4a6eb57

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

src/Controllers/UserController.js

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,30 @@ export class UserController extends AdaptableController {
8181
}
8282

8383
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+
}
9597

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';
100104
}
101-
if (expiresDate < new Date()) throw 'The password reset link has expired';
102-
}
103-
return results[0];
104-
});
105+
106+
return results[0];
107+
});
105108
}
106109

107110
getUserIfNeeded(user) {
@@ -208,10 +211,24 @@ export class UserController extends AdaptableController {
208211
this.config.passwordPolicy.resetTokenReuseIfValid &&
209212
this.config.passwordPolicy.resetTokenValidityDuration
210213
) {
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+
}
215232
}
216233
}
217234
if (!user || !user._perishable_token) {

0 commit comments

Comments
 (0)