Skip to content

Commit

Permalink
feat: make validatePassword async and pass user to args
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Nov 26, 2023
1 parent 87fa913 commit d6faf8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-donkeys-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@accounts/password': major
---

Make validatePassword async and pass user to args
25 changes: 13 additions & 12 deletions packages/password/src/accounts-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export interface AccountsPasswordOptions {
* Function that check if the password is valid.
* This function will be called when you call `createUser` and `changePassword`.
*/
validatePassword?: (password?: string) => boolean;
validatePassword?: <T extends User>(password?: string, user?: T) => Promise<boolean>;
/**
* Function that check if the username is a valid username.
* This function will be called when you call `createUser`.
Expand Down Expand Up @@ -164,7 +164,7 @@ const defaultOptions = {
validateEmail(email?: string): boolean {
return isString(email) && isEmail(email);
},
validatePassword(password?: string): boolean {
async validatePassword(password?: string): Promise<boolean> {
return isString(password) && password !== '';
},
validateUsername(username?: string): boolean {
Expand Down Expand Up @@ -365,12 +365,6 @@ export default class AccountsPassword<CustomUser extends User = User>
if (!token || !isString(token)) {
throw new AccountsJsError(this.options.errors.invalidToken, ResetPasswordErrors.InvalidToken);
}
if (!this.options.validatePassword(newPassword)) {
throw new AccountsJsError(
this.options.errors.invalidNewPassword,
ResetPasswordErrors.InvalidNewPassword
);
}

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

if (!(await this.options.validatePassword(newPassword, user))) {
throw new AccountsJsError(
this.options.errors.invalidNewPassword,
ResetPasswordErrors.InvalidNewPassword
);
}

const resetTokens = getUserResetTokens(user);
const resetTokenRecord = resetTokens.find((t) => t.token === token);

Expand Down Expand Up @@ -471,15 +472,15 @@ export default class AccountsPassword<CustomUser extends User = User>
oldPassword: string,
newPassword: string
): Promise<void> {
if (!this.options.validatePassword(newPassword)) {
const user = await this.passwordAuthenticator({ id: userId }, oldPassword);

if (!(await this.options.validatePassword(newPassword, user))) {
throw new AccountsJsError(
this.options.errors.invalidPassword,
ChangePasswordErrors.InvalidPassword
);
}

const user = await this.passwordAuthenticator({ id: userId }, oldPassword);

const password = await this.options.hashPassword(newPassword);
await this.db.setPassword(userId, password);

Expand Down Expand Up @@ -676,7 +677,7 @@ export default class AccountsPassword<CustomUser extends User = User>
}

if (user.password) {
if (!this.options.validatePassword(user.password)) {
if (!(await this.options.validatePassword(user.password))) {
throw new AccountsJsError(
this.options.errors.invalidPassword,
CreateUserErrors.InvalidPassword
Expand Down

0 comments on commit d6faf8d

Please sign in to comment.