Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(web): carry the typed email over to the password reset form (#18585)

Der Gerätc895a3d56a9d
diff --git a/web/src/__tests__/auth-sign-in-page.clienttest.tsx b/web/src/__tests__/auth-sign-in-page.clienttest.tsx
index 66230d948b..dce3f002f6 100644
--- a/web/src/__tests__/auth-sign-in-page.clienttest.tsx
+++ b/web/src/__tests__/auth-sign-in-page.clienttest.tsx
@@ -7,7 +7,7 @@ const { captureExceptionMock, addBreadcrumbMock, signInMock, routerState } =
captureExceptionMock: vi.fn(),
addBreadcrumbMock: vi.fn(),
signInMock: vi.fn(),
- routerState: { query: {} as Record<string, string> },
+ routerState: { query: {} as Record<string, string | string[]> },
}));
vi.mock("@sentry/nextjs", () => ({
@@ -522,3 +522,71 @@ describe("sign-in page last used SSO email", () => {
expect(storedSsoEmail()).toBe("[email protected]");
});
});
+
+// The address is already on screen when "(forgot password?)" is clicked, so the
+// reset page should not ask for it a second time (langfuse#18583).
+describe("sign-in page forgot-password link", () => {
+ beforeEach(() => {
+ signInMock.mockReset();
+ routerState.query = {};
+ window.localStorage.clear();
+ });
+
+ const forgotPasswordLink = () =>
+ screen.getByRole("link", { name: /forgot password/i });
+
+ it("links to the bare reset page while the email field is empty", () => {
+ renderSignIn();
+
+ expect(forgotPasswordLink()).toHaveAttribute(
+ "href",
+ "/auth/reset-password",
+ );
+ });
+
+ it("carries the typed address over, percent-encoded", () => {
+ renderSignIn();
+
+ // A `+` is legal in an address and decodes back as a space unencoded.
+ fireEvent.change(screen.getByLabelText("Email"), {
+ target: { value: " [email protected] " },
+ });
+
+ expect(forgotPasswordLink()).toHaveAttribute(
+ "href",
+ "/auth/reset-password?email=jane%2Btest%40example.com",
+ );
+ });
+
+ it("carries over an address restored from the query", () => {
+ routerState.query = { email: "[email protected]" };
+
+ renderSignIn();
+
+ expect(forgotPasswordLink()).toHaveAttribute(
+ "href",
+ "/auth/reset-password?email=jane%40example.com",
+ );
+ });
+});
+
+// `?email=a&email=b` makes router.query.email an array. Rendering it would put
+// "a,b" in the field; reading it as a string would throw on an array.
+describe("sign-in page repeated ?email= param", () => {
+ beforeEach(() => {
+ signInMock.mockReset();
+ routerState.query = {};
+ window.localStorage.clear();
+ });
+
+ it("ignores the param rather than crashing or joining the values", () => {
+ routerState.query = { email: ["[email protected]", "[email protected]"] };
+
+ renderSignIn();
+
+ expect(screen.getByLabelText("Email")).toHaveValue("");
+ expect(
+ screen.getByRole("link", { name: /forgot password/i }),
+ ).toHaveAttribute("href", "/auth/reset-password");
+ });
+});
diff --git a/web/src/features/auth-credentials/ResetPasswordAuthPage.tsx b/web/src/features/auth-credentials/ResetPasswordAuthPage.tsx
index be55d23cd2..df3bb587db 100644
--- a/web/src/features/auth-credentials/ResetPasswordAuthPage.tsx
+++ b/web/src/features/auth-credentials/ResetPasswordAuthPage.tsx
@@ -1,3 +1,4 @@
+import { useRouter } from "next/router";
import { ResetPasswordPage } from "@/src/features/auth-credentials/components/ResetPasswordPage";
type PageProps = {
@@ -7,5 +8,17 @@ type PageProps = {
export default function ResetPasswordAuthPage({
passwordResetAvailable,
}: PageProps) {
- return <ResetPasswordPage passwordResetAvailable={passwordResetAvailable} />;
+ const router = useRouter();
+ /** Prefills the email from `?email=`, which the sign-in page's reset link sets. */
+ // getServerSideProps keeps the query populated on the first render, which is
+ // what seeding ResetPasswordPage's useState depends on. A repeated param
+ // arrives as an array and is dropped.
+ const emailParam = router.query.email;
+
+ return (
+ <ResetPasswordPage
+ passwordResetAvailable={passwordResetAvailable}
+ initialEmail={typeof emailParam === "string" ? emailParam : ""}
+ />
+ );
}
diff --git a/web/src/features/auth-credentials/components/ResetPasswordPage.clienttest.tsx b/web/src/features/auth-credentials/components/ResetPasswordPage.clienttest.tsx
index 48c0a44db8..a1d2fa20de 100644
--- a/web/src/features/auth-credentials/components/ResetPasswordPage.clienttest.tsx
+++ b/web/src/features/auth-credentials/components/ResetPasswordPage.clienttest.tsx
@@ -20,7 +20,7 @@ const {
routerPushMock: vi.fn(),
routerState: {
isReady: true,
- query: {} as Record<string, string>,
+ query: {} as Record<string, string | string[]>,
},
useLangfuseCloudRegionMock: vi.fn(),
useSessionMock: vi.fn(),
@@ -64,13 +64,15 @@ vi.mock(
"@/src/features/auth-credentials/components/ResetPasswordButton",
() => ({
RequestResetPasswordEmailButton: ({
+ email,
onEmailSent,
label = "Send email",
}: {
+ email?: string;
onEmailSent?: () => void;
label?: string;
}) => (
- <button type="button" onClick={() => onEmailSent?.()}>
+ <button type="button" data-email={email} onClick={() => onEmailSent?.()}>
{label}
</button>
),
@@ -78,6 +80,7 @@ vi.mock(
);
import { ResetPasswordPage } from "@/src/features/auth-credentials/components/ResetPasswordPage";
+import ResetPasswordAuthPage from "@/src/features/auth-credentials/ResetPasswordAuthPage";
const submitPasswordForm = ({
code,
@@ -250,3 +253,58 @@ describe("ResetPasswordPage re-authentication", () => {
);
});
});
+
+describe("reset-password page ?email= prefill", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ routerState.isReady = true;
+ routerState.query = {};
+ useLangfuseCloudRegionMock.mockReturnValue({
+ isLangfuseCloud: false,
+ region: undefined,
+ });
+ useSessionMock.mockReturnValue({ status: "unauthenticated", data: null });
+ });
+
+ it("prefills the email input from the query and hands it to the request button", () => {
+ routerState.query = { email: "[email protected]" };
+
+ render(<ResetPasswordAuthPage passwordResetAvailable />);
+
+ expect(screen.getByLabelText("Email")).toHaveValue("[email protected]");
+ // The mocked request button above stands in for RequestResetPasswordEmailButton.
+ expect(screen.getByRole("button", { name: "Send email" })).toHaveAttribute(
+ "data-email",
+ );
+ });
+
+ it("only prefills — the reset email still takes an explicit click", () => {
+ routerState.query = { email: "[email protected]" };
+
+ render(<ResetPasswordAuthPage passwordResetAvailable />);
+
+ // Anchor the absences below: without this the test also passes when the
+ // prefill does nothing at all.
+ expect(screen.getByLabelText("Email")).toHaveValue("[email protected]");
+ // Jumping straight to the code step would imply an email had been sent.
+ expect(
+ screen.queryByLabelText("Verification code"),
+ ).not.toBeInTheDocument();
+ expect(signInMock).not.toHaveBeenCalled();
+ });
+
+ it("leaves the field empty without the param", () => {
+ render(<ResetPasswordAuthPage passwordResetAvailable />);
+
+ expect(screen.getByLabelText("Email")).toHaveValue("");
+ });
+
+ it("ignores a repeated ?email= rather than joining the values", () => {
+ routerState.query = { email: ["[email protected]", "[email protected]"] };
+
+ render(<ResetPasswordAuthPage passwordResetAvailable />);
+
+ expect(screen.getByLabelText("Email")).toHaveValue("");
+ });
+});
diff --git a/web/src/features/auth-credentials/components/ResetPasswordPage.tsx b/web/src/features/auth-credentials/components/ResetPasswordPage.tsx
index 091674b25e..fa2dfe9d4f 100644
--- a/web/src/features/auth-credentials/components/ResetPasswordPage.tsx
+++ b/web/src/features/auth-credentials/components/ResetPasswordPage.tsx
@@ -64,6 +64,8 @@ export function ResetPasswordPage({
const [isSuccess, setIsSuccess] = useState(false);
const [email, setEmail] = useState(initialEmail);
const [codeRequested, setCodeRequested] = useState(
+ // The intent check keeps a URL-supplied initialEmail from opening the code
+ // step for an address no email was ever sent to.
intent === "setup" && Boolean(initialEmail),
);
diff --git a/web/src/features/auth/SignInPage.tsx b/web/src/features/auth/SignInPage.tsx
index 1b22f9e343..913aa3c45e 100644
--- a/web/src/features/auth/SignInPage.tsx
+++ b/web/src/features/auth/SignInPage.tsx
@@ -577,9 +577,11 @@ export default function SignInPage({
);
const hasMultipleAuthMethods = availableProviders.length > 1;
- // Read query params for targetPath and email pre-population
+ // Read query params for targetPath and email pre-population. A repeated
+ // `?email=` arrives as an array; dropping it keeps the form default a string.
const queryTargetPath = router.query.targetPath as string | undefined;
- const emailParam = router.query.email as string | undefined;
+ const queryEmail = router.query.email;
+ const emailParam = typeof queryEmail === "string" ? queryEmail : undefined;
// Validate targetPath to prevent open redirect attacks
const targetPath = queryTargetPath
@@ -619,6 +621,12 @@ export default function SignInPage({
setLastUsedSsoEmail,
]);
+ /** Reset-password link carrying the typed address for that page to prefill. */
+ const typedEmail = credentialsForm.watch("email").trim();
+ const resetPasswordHref = typedEmail
+ ? `/auth/reset-password?email=${encodeURIComponent(typedEmail)}`
+ : "/auth/reset-password";
+
async function onCredentialsSubmit(
values: z.infer<typeof credentialAuthForm>,
) {
@@ -909,7 +917,7 @@ export default function SignInPage({
<FormLabel>
Password{" "}
<Link
- href="/auth/reset-password"
+ href={resetPasswordHref}
className="text-link hover:text-link-hover ml-1 text-xs"
tabIndex={-1}
title="What is this?"