Skip to content

Commit 9e3c2ac

Browse files
authored
fix: call validatePassword cb on password change (#183)
* fix: call validatePassword cb on password change * Call password validation callback on password reset
1 parent b0e48eb commit 9e3c2ac

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

.changeset/early-weeks-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@openauthjs/openauth": patch
3+
---
4+
5+
Call password validation callback on password reset

packages/openauth/src/provider/password.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,32 @@ export function PasswordProvider(
490490
if (password !== repeat)
491491
return transition(provider, { type: "password_mismatch" })
492492

493+
if (config.validatePassword) {
494+
let validationError: string | undefined
495+
try {
496+
if (typeof config.validatePassword === "function") {
497+
validationError = await config.validatePassword(password)
498+
} else {
499+
const res =
500+
await config.validatePassword["~standard"].validate(password)
501+
502+
if (res.issues?.length) {
503+
throw new Error(
504+
res.issues.map((issue) => issue.message).join(", "),
505+
)
506+
}
507+
}
508+
} catch (error) {
509+
validationError =
510+
error instanceof Error ? error.message : undefined
511+
}
512+
if (validationError)
513+
return transition(provider, {
514+
type: "validation_error",
515+
message: validationError,
516+
})
517+
}
518+
493519
await Storage.set(
494520
ctx.storage,
495521
["email", provider.email, "password"],

0 commit comments

Comments
 (0)