feat(plugin): error code support for haveibeenpwned plugin

feat(password): error code support for haveibeenpwned
This commit is contained in:
KinfeMichael Tariku
2025-04-13 14:21:00 +03:00
committed by GitHub
3 changed files with 11 additions and 5 deletions

View File

@@ -5,7 +5,7 @@ import useMeasure from "react-use-measure";
import Link from "next/link";
import clsx from "clsx";
import { Button } from "@/components/ui/button";
import { Check, Copy, CornerRightUp } from "lucide-react";
import { Check, Copy } from "lucide-react";
import { useTheme } from "next-themes";
import { Highlight, themes } from "prism-react-renderer";
import { AnimatePresence, motion, MotionConfig } from "framer-motion";

View File

@@ -1,7 +1,6 @@
import { describe, expect, it } from "vitest";
import { getTestInstance } from "../../test-utils/test-instance";
import { haveIBeenPwned } from "./index";
describe("have-i-been-pwned", async () => {
const { client } = await getTestInstance(
{
@@ -23,6 +22,9 @@ describe("have-i-been-pwned", async () => {
});
expect(result.error).not.toBeNull();
expect(result.error?.status).toBe(400);
expect(result.error?.code).toBe(
"THE_PASSWORD_YOU_ENTERED_HAS_BEEN_COMPROMISED_PLEASE_CHOOSE_A_DIFFERENT_PASSWORD",
);
});
it("should allow account creation with strong, uncompromised password", async () => {
@@ -46,7 +48,6 @@ describe("have-i-been-pwned", async () => {
password: initialPassword,
name: "Test User",
});
console.log(res);
const result = await client.changePassword(
{
currentPassword: initialPassword,
@@ -58,7 +59,6 @@ describe("have-i-been-pwned", async () => {
},
},
);
console.log({ result });
expect(result.error).toBeDefined();
expect(result.error?.status).toBe(400);
});

View File

@@ -3,6 +3,11 @@ import { createHash } from "@better-auth/utils/hash";
import { betterFetch } from "@better-fetch/fetch";
import type { BetterAuthPlugin } from "../../types";
const ERROR_CODES = {
PASSWORD_COMPROMISED:
"THE_PASSWORD_YOU_ENTERED_HAS_BEEN_COMPROMISED_PLEASE_CHOOSE_A_DIFFERENT_PASSWORD",
} as const;
async function checkPasswordCompromise(
password: string,
customMessage?: string,
@@ -30,7 +35,6 @@ async function checkPasswordCompromise(
message: `Failed to check password. Status: ${error.status}`,
});
}
const lines = data.split("\n");
const found = lines.some(
(line) => line.split(":")[0].toUpperCase() === suffix.toUpperCase(),
@@ -41,6 +45,7 @@ async function checkPasswordCompromise(
message:
customMessage ||
"The password you entered has been compromised. Please choose a different password.",
code: ERROR_CODES.PASSWORD_COMPROMISED,
});
}
} catch (error) {
@@ -74,4 +79,5 @@ export const haveIBeenPwned = (options?: HaveIBeenPwnedOptions) =>
},
};
},
$ERROR_CODES: ERROR_CODES,
}) satisfies BetterAuthPlugin;