Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make validatePassword async and pass user to args #1265

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading