Skip to content

Commit 88f017d

Browse files
committed
feat: make validatePassword async and pass user to args
1 parent 87fa913 commit 88f017d

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

.changeset/perfect-donkeys-study.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@accounts/password': minor
3+
---
4+
5+
Make validatePassword async and pass user to args

packages/password/src/accounts-password.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export interface AccountsPasswordOptions {
127127
* Function that check if the password is valid.
128128
* This function will be called when you call `createUser` and `changePassword`.
129129
*/
130-
validatePassword?: (password?: string) => boolean;
130+
validatePassword?: <T extends User>(password?: string, user?: T) => Promise<boolean>;
131131
/**
132132
* Function that check if the username is a valid username.
133133
* This function will be called when you call `createUser`.
@@ -164,7 +164,7 @@ const defaultOptions = {
164164
validateEmail(email?: string): boolean {
165165
return isString(email) && isEmail(email);
166166
},
167-
validatePassword(password?: string): boolean {
167+
async validatePassword(password?: string): Promise<boolean> {
168168
return isString(password) && password !== '';
169169
},
170170
validateUsername(username?: string): boolean {
@@ -365,12 +365,6 @@ export default class AccountsPassword<CustomUser extends User = User>
365365
if (!token || !isString(token)) {
366366
throw new AccountsJsError(this.options.errors.invalidToken, ResetPasswordErrors.InvalidToken);
367367
}
368-
if (!this.options.validatePassword(newPassword)) {
369-
throw new AccountsJsError(
370-
this.options.errors.invalidNewPassword,
371-
ResetPasswordErrors.InvalidNewPassword
372-
);
373-
}
374368

375369
const user = await this.db.findUserByResetPasswordToken(token);
376370
if (!user) {
@@ -380,6 +374,13 @@ export default class AccountsPassword<CustomUser extends User = User>
380374
);
381375
}
382376

377+
if (!(await this.options.validatePassword(newPassword, user))) {
378+
throw new AccountsJsError(
379+
this.options.errors.invalidNewPassword,
380+
ResetPasswordErrors.InvalidNewPassword
381+
);
382+
}
383+
383384
const resetTokens = getUserResetTokens(user);
384385
const resetTokenRecord = resetTokens.find((t) => t.token === token);
385386

@@ -471,15 +472,15 @@ export default class AccountsPassword<CustomUser extends User = User>
471472
oldPassword: string,
472473
newPassword: string
473474
): Promise<void> {
474-
if (!this.options.validatePassword(newPassword)) {
475+
const user = await this.passwordAuthenticator({ id: userId }, oldPassword);
476+
477+
if (!(await this.options.validatePassword(newPassword, user))) {
475478
throw new AccountsJsError(
476479
this.options.errors.invalidPassword,
477480
ChangePasswordErrors.InvalidPassword
478481
);
479482
}
480483

481-
const user = await this.passwordAuthenticator({ id: userId }, oldPassword);
482-
483484
const password = await this.options.hashPassword(newPassword);
484485
await this.db.setPassword(userId, password);
485486

@@ -676,7 +677,7 @@ export default class AccountsPassword<CustomUser extends User = User>
676677
}
677678

678679
if (user.password) {
679-
if (!this.options.validatePassword(user.password)) {
680+
if (!(await this.options.validatePassword(user.password))) {
680681
throw new AccountsJsError(
681682
this.options.errors.invalidPassword,
682683
CreateUserErrors.InvalidPassword

0 commit comments

Comments
 (0)