Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Say a password is too long instead of too short or 'something went wrong'

Claude3b3f9b0bd2a1
diff --git a/src/components/auth/login-shell.tsx b/src/components/auth/login-shell.tsx
index 14d5923..a4ca58d 100644
--- a/src/components/auth/login-shell.tsx
+++ b/src/components/auth/login-shell.tsx
@@ -12,6 +12,11 @@ import {
} from "@/components/auth/sso-button";
import { FieldShell, ShellButton } from "@/components/shared/form-shell";
import { authClient } from "@/lib/auth/client";
+import {
+ describePasswordRefusal,
+ newPasswordLengthError,
+ PASSWORD_TOO_LONG_MESSAGE,
+} from "@/lib/password-messages";
import { forgotPasswordSchema, loginSchema } from "@/lib/schemas/auth";
import {
Dialog,
@@ -258,6 +263,12 @@ function LiveLoginShell({
} else if (normalizedErrorMessage.includes("email not verified")) {
setBannerMessage(verificationRequiredMessage);
setShowResendVerification(true);
+ } else if (
+ describePasswordRefusal(result.error) === PASSWORD_TOO_LONG_MESSAGE
+ ) {
+ // No account has a password this long, so it cannot be right. Say
+ // why instead of "Something went wrong" (issue #57).
+ setErrors({ password: PASSWORD_TOO_LONG_MESSAGE });
} else {
setBannerMessage(
normalizedErrorMessage.includes("invalid email or password")
@@ -632,8 +643,10 @@ function ResetPasswordCard({ resetToken }: { resetToken: string }) {
formData.get("reset-password") ?? password,
).trim();
- if (trimmedPassword.length < 15) {
- setError("Password must be at least 15 characters");
+ const lengthError = newPasswordLengthError(trimmedPassword);
+
+ if (lengthError) {
+ setError(lengthError);
setSuccessMessage(null);
return;
}
@@ -654,11 +667,8 @@ function ResetPasswordCard({ resetToken }: { resetToken: string }) {
setError(
normalizedErrorMessage.includes("invalid token")
? "This reset link is invalid or has expired."
- : normalizedErrorMessage.includes("password too short")
- ? "Password must be at least 15 characters"
- : normalizedErrorMessage.includes("compromised")
- ? "Choose a password that hasn't appeared in a breach"
- : "Something went wrong. Please try again.",
+ : (describePasswordRefusal(result.error) ??
+ "Something went wrong. Please try again."),
);
setIsSubmitting(false);
return;
diff --git a/src/components/auth/signup-shell.tsx b/src/components/auth/signup-shell.tsx
index 3dc81c4..b8f23bc 100644
--- a/src/components/auth/signup-shell.tsx
+++ b/src/components/auth/signup-shell.tsx
@@ -11,6 +11,11 @@ import {
import { SsoButton, type SsoOption } from "@/components/auth/sso-button";
import { FieldShell, ShellButton } from "@/components/shared/form-shell";
import { authClient } from "@/lib/auth/client";
+import {
+ describePasswordRefusal,
+ newPasswordLengthError,
+ PASSWORD_TOO_SHORT_MESSAGE,
+} from "@/lib/password-messages";
import { signUpSchema } from "@/lib/schemas/auth";
const defaultSuccessMessage = "Check your email to verify your sender account.";
@@ -380,7 +385,9 @@ function LiveSignupShell({
}
if (field === "password") {
- nextErrors.password = "Password must be at least 15 characters";
+ nextErrors.password =
+ newPasswordLengthError(trimmedValues.password) ??
+ PASSWORD_TOO_SHORT_MESSAGE;
}
}
@@ -420,13 +427,9 @@ function LiveSignupShell({
setErrors({
email: "This email is already registered",
});
- } else if (normalizedErrorMessage.includes("password too short")) {
- setErrors({
- password: "Password must be at least 15 characters",
- });
- } else if (normalizedErrorMessage.includes("compromised")) {
+ } else if (describePasswordRefusal(result.error)) {
setErrors({
- password: "Choose a password that hasn't appeared in a breach",
+ password: describePasswordRefusal(result.error)!,
});
} else {
setBannerMessage("Something went wrong. Please try again.");
diff --git a/src/lib/password-messages.test.ts b/src/lib/password-messages.test.ts
new file mode 100644
index 0000000..5c3147b
--- /dev/null
+++ b/src/lib/password-messages.test.ts
@@ -0,0 +1,59 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import {
+ describePasswordRefusal,
+ newPasswordLengthError,
+ PASSWORD_BREACHED_MESSAGE,
+ PASSWORD_TOO_LONG_MESSAGE,
+ PASSWORD_TOO_SHORT_MESSAGE,
+} from "./password-messages.ts";
+
+const overLimit = `Ab7!${"rT9#".repeat(31)}Z`;
+
+test("a new password over 128 characters is told the maximum, not the minimum (issue #57)", () => {
+ assert.equal(overLimit.length, 129);
+ assert.equal(newPasswordLengthError(overLimit), PASSWORD_TOO_LONG_MESSAGE);
+ assert.equal(newPasswordLengthError(overLimit.slice(0, 128)), null);
+ assert.equal(newPasswordLengthError("a".repeat(15)), null);
+ assert.equal(
+ newPasswordLengthError("a".repeat(14)),
+ PASSWORD_TOO_SHORT_MESSAGE,
+ );
+});
+
+test("Better Auth's password refusals get their own messages", () => {
+ assert.equal(
+ describePasswordRefusal({
+ code: "PASSWORD_TOO_LONG",
+ message: "Password too long",
+ }),
+ PASSWORD_TOO_LONG_MESSAGE,
+ );
+ assert.equal(
+ describePasswordRefusal({ message: "Password too long" }),
+ PASSWORD_TOO_LONG_MESSAGE,
+ );
+ assert.equal(
+ describePasswordRefusal({
+ code: "PASSWORD_TOO_SHORT",
+ message: "Password too short",
+ }),
+ PASSWORD_TOO_SHORT_MESSAGE,
+ );
+ assert.equal(
+ describePasswordRefusal({
+ code: "PASSWORD_COMPROMISED",
+ message: "The password you entered has been compromised.",
+ }),
+ PASSWORD_BREACHED_MESSAGE,
+ );
+ assert.equal(
+ describePasswordRefusal({
+ code: "INVALID_EMAIL_OR_PASSWORD",
+ message: "Invalid email or password",
+ }),
+ null,
+ );
+ assert.equal(describePasswordRefusal(null), null);
+});
diff --git a/src/lib/password-messages.ts b/src/lib/password-messages.ts
new file mode 100644
index 0000000..ce11b29
--- /dev/null
+++ b/src/lib/password-messages.ts
@@ -0,0 +1,53 @@
+// What a person is told about a password's length, the same on every form.
+// The limits match Better Auth's minPasswordLength and maxPasswordLength in
+// src/lib/auth/server.ts (15 to 128 characters).
+
+export const PASSWORD_MIN_LENGTH = 15;
+export const PASSWORD_MAX_LENGTH = 128;
+
+export const PASSWORD_TOO_SHORT_MESSAGE = `Password must be at least ${PASSWORD_MIN_LENGTH} characters`;
+export const PASSWORD_TOO_LONG_MESSAGE = `Password must be at most ${PASSWORD_MAX_LENGTH} characters`;
+export const PASSWORD_BREACHED_MESSAGE =
+ "Choose a password that hasn't appeared in a breach";
+
+/** The length problem with a new password, or null when it fits. */
+export function newPasswordLengthError(password: string): string | null {
+ if (password.length < PASSWORD_MIN_LENGTH) {
+ return PASSWORD_TOO_SHORT_MESSAGE;
+ }
+
+ if (password.length > PASSWORD_MAX_LENGTH) {
+ return PASSWORD_TOO_LONG_MESSAGE;
+ }
+
+ return null;
+}
+
+type AuthErrorLike = { code?: string | null; message?: string | null };
+
+/**
+ * The message for a password Better Auth refused, or null when the refusal
+ * was not about the password itself. A password over the limit is answered
+ * with PASSWORD_TOO_LONG / "Password too long", which used to end up as
+ * "Something went wrong" (issue #57).
+ */
+export function describePasswordRefusal(
+ error: AuthErrorLike | null | undefined,
+): string | null {
+ const code = error?.code ?? "";
+ const message = error?.message?.trim().toLowerCase() ?? "";
+
+ if (code === "PASSWORD_TOO_LONG" || message.includes("password too long")) {
+ return PASSWORD_TOO_LONG_MESSAGE;
+ }
+
+ if (code === "PASSWORD_TOO_SHORT" || message.includes("password too short")) {
+ return PASSWORD_TOO_SHORT_MESSAGE;
+ }
+
+ if (message.includes("compromised")) {
+ return PASSWORD_BREACHED_MESSAGE;
+ }
+
+ return null;
+}