AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): clear the signup page's loading-reset timer on unmount (#15423)
Reinier van der Leere9a6981d218d
diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/signup/__tests__/page.test.tsx b/autogpt_platform/frontend/src/app/(no-navbar)/signup/__tests__/page.test.tsxindex 49ddcf9987..337f74cbc8 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/signup/__tests__/page.test.tsx+++ b/autogpt_platform/frontend/src/app/(no-navbar)/signup/__tests__/page.test.tsx@@ -98,6 +98,7 @@ describe("SignupPage", () => {afterEach(() => {vi.unstubAllEnvs();vi.restoreAllMocks();+ vi.useRealTimers();});test("shows existing user feedback from signup action", async () => {@@ -476,6 +477,38 @@ describe("SignupPage", () => {]);});+ test("leaves no timer pending once it unmounts after a signup attempt", async () => {+ vi.useFakeTimers({ shouldAdvanceTime: true });+ const { unmount } = render(<SignupPage />);++ fillValidForm();+ fireEvent.click(screen.getByRole("button", { name: "Sign up" }));+ await screen.findByText("User with this email already exists");+ unmount();++ expect(vi.getTimerCount()).toBe(0);+ });++ test("leaves no timer pending when it unmounts before a signup attempt settles", async () => {+ vi.useFakeTimers({ shouldAdvanceTime: true });+ let settle: (result: unknown) => void = () => {};+ mockSignupAction.mockReturnValue(+ new Promise((resolve) => {+ settle = resolve;+ }),+ );+ const { unmount } = render(<SignupPage />);++ fillValidForm();+ fireEvent.click(screen.getByRole("button", { name: "Sign up" }));+ await waitFor(() => expect(mockSignupAction).toHaveBeenCalledTimes(1));+ unmount();+ settle({ success: false, error: "user_already_exists" });+ await new Promise((resolve) => setTimeout(resolve, 0));++ expect(vi.getTimerCount()).toBe(0);+ });+test("does not link to the demo tour", () => {render(<SignupPage />);diff --git a/autogpt_platform/frontend/src/app/(no-navbar)/signup/useSignupPage.ts b/autogpt_platform/frontend/src/app/(no-navbar)/signup/useSignupPage.tsindex 4889a35033..828ebcda7a 100644--- a/autogpt_platform/frontend/src/app/(no-navbar)/signup/useSignupPage.ts+++ b/autogpt_platform/frontend/src/app/(no-navbar)/signup/useSignupPage.ts@@ -8,7 +8,7 @@ import { environment } from "@/services/environment";import { LoginProvider, signupFormSchema } from "@/types/auth";import { zodResolver } from "@hookform/resolvers/zod";import { useRouter, useSearchParams } from "next/navigation";-import { useEffect, useState } from "react";+import { useEffect, useRef, useState } from "react";import { useForm } from "react-hook-form";import z from "zod";import { signup as signupAction } from "./actions";@@ -30,11 +30,21 @@ export function useSignupPage() {null,);const isCloudEnv = environment.isCloud();+ const loadingResetTimer = useRef<ReturnType<typeof setTimeout> | null>(null);+ const isMounted = useRef(false);// Same-origin redirect target; off-site values are dropped so a crafted// `/signup?next=https://phishing.site` cannot redirect users elsewhere.const nextUrl = sanitizeAuthNext(searchParams.get("next"));+ useEffect(() => {+ isMounted.current = true;+ return () => {+ isMounted.current = false;+ if (loadingResetTimer.current) clearTimeout(loadingResetTimer.current);+ };+ }, []);+useEffect(() => {if (!isUserLoading) {setHasInitializedAuth(true);@@ -190,9 +200,13 @@ export function useSignupPage() {variant: "destructive",});} finally {- setTimeout(() => {- setIsLoading(false);- }, 3000);+ if (loadingResetTimer.current) clearTimeout(loadingResetTimer.current);+ // A signup that settles after unmount must not leave a timer behind.+ if (isMounted.current) {+ loadingResetTimer.current = setTimeout(() => {+ setIsLoading(false);+ }, 3000);+ }}}