Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(auth): retry deferred handoff after effect replay

dajiaohuang0e6a658790c7
diff --git a/web/app/handoff/page.tsx b/web/app/handoff/page.tsx
index 1b1dbf9ee..8adb5616b 100644
--- a/web/app/handoff/page.tsx
+++ b/web/app/handoff/page.tsx
@@ -40,8 +40,10 @@ function HandoffPageContent() {
const code = searchParams.get("code")?.trim() ?? "";
window.history.replaceState(null, "", window.location.pathname);
if (!code || started.current) return;
- started.current = true;
const timer = window.setTimeout(() => {
+ // Effect cleanup may cancel this deferred start before it runs.
+ // Mark the exchange as started only when it actually begins.
+ started.current = true;
void finish(code);
}, 0);
return () => window.clearTimeout(timer);
diff --git a/web/tests/handoff-effect-replay.spec.tsx b/web/tests/handoff-effect-replay.spec.tsx
new file mode 100644
index 000000000..24ace3ff0
--- /dev/null
+++ b/web/tests/handoff-effect-replay.spec.tsx
@@ -0,0 +1,38 @@
+import { render, waitFor } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+import HandoffPage from "@/app/handoff/page";
+
+const runtime = vi.hoisted(() => ({ fetch: vi.fn(), replace: vi.fn() }));
+vi.mock("@/lib/api", () => ({ apiFetch: runtime.fetch, apiUrl: (url: string) => url }));
+vi.mock("next/navigation", () => ({
+ useRouter: () => ({ replace: runtime.replace }),
+ useSearchParams: () => new URLSearchParams("code=example-code&next=%2Fprofile"),
+}));
+vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (value: string) => value }) }));
+
+describe("automatic handoff sign-in", () => {
+ it.each([false, true])("completes a code sign-in with strict mode %s", async strict => {
+ runtime.replace.mockReset();
+ runtime.fetch.mockReset().mockImplementation((url: string) => Promise.resolve(
+ new Response(JSON.stringify(url.endsWith("/exchange") ? { ticket: "example-ticket" } : {})),
+ ));
+ render(<HandoffPage />, { reactStrictMode: strict });
+ await waitFor(() => expect(runtime.replace).toHaveBeenCalledWith("/profile"));
+ expect(runtime.fetch).toHaveBeenCalledTimes(2);
+ expect(JSON.parse(runtime.fetch.mock.calls[0][1].body)).toEqual({ code: "example-code" });
+ expect(JSON.parse(runtime.fetch.mock.calls[1][1].body)).toEqual({ ticket: "example-ticket" });
+ });
+
+ it("does not exchange the code after unmount before the deferred start", async () => {
+ runtime.fetch.mockReset();
+ vi.useFakeTimers();
+ try {
+ const { unmount } = render(<HandoffPage />);
+ unmount();
+ await vi.runAllTimersAsync();
+ expect(runtime.fetch).not.toHaveBeenCalled();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+});