Skip to content

Commit e777bef

Browse files
authored
fix(plugin): prevent user from being created on compromised password
fix(plugin): prevent user from being created on compromised password
2 parents 5689df6 + c9ac010 commit e777bef

File tree

1 file changed

+19
-0
lines changed
  • packages/better-auth/src/plugins/haveibeenpwned

1 file changed

+19
-0
lines changed

packages/better-auth/src/plugins/haveibeenpwned/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { APIError } from "../../api";
22
import { createHash } from "@better-auth/utils/hash";
33
import { betterFetch } from "@better-fetch/fetch";
44
import type { BetterAuthPlugin } from "../../types";
5+
import { createAuthMiddleware } from "../../api";
56

67
const ERROR_CODES = {
78
PASSWORD_COMPROMISED:
@@ -63,6 +64,24 @@ export interface HaveIBeenPwnedOptions {
6364
export const haveIBeenPwned = (options?: HaveIBeenPwnedOptions) =>
6465
({
6566
id: "haveIBeenPwned",
67+
hooks: {
68+
before: [
69+
{
70+
matcher(ctx) {
71+
return ctx.path === "/sign-up/email";
72+
},
73+
handler: createAuthMiddleware(async (ctx) => {
74+
if (ctx.body?.password) {
75+
await checkPasswordCompromise(
76+
ctx.body.password,
77+
options?.customPasswordCompromisedMessage,
78+
);
79+
}
80+
return ctx;
81+
}),
82+
},
83+
],
84+
},
6685
init(ctx) {
6786
return {
6887
context: {

0 commit comments

Comments
 (0)