diff --git a/apps/web/src/browserAuth.test.ts b/apps/web/src/browserAuth.test.ts index a82a86c21d..0d500b9d40 100644 --- a/apps/web/src/browserAuth.test.ts +++ b/apps/web/src/browserAuth.test.ts @@ -123,6 +123,45 @@ describe("browserAuth", () => { expect(window.location.hash).toBe(""); }); + it("polls auth session after a successful bootstrap until the cookie is visible", async () => { + setTestWindow("http://localhost:3773/#t3_bootstrap=pair-me"); + fetchMock + .mockResolvedValueOnce( + new Response(null, { + status: 204, + }), + ) + .mockResolvedValueOnce( + new Response(JSON.stringify({ authenticated: false }), { + status: 200, + headers: { + "Content-Type": "application/json", + }, + }), + ) + .mockResolvedValueOnce( + new Response(JSON.stringify({ authenticated: false }), { + status: 200, + headers: { + "Content-Type": "application/json", + }, + }), + ) + .mockResolvedValueOnce( + new Response(JSON.stringify({ authenticated: true }), { + status: 200, + headers: { + "Content-Type": "application/json", + }, + }), + ); + + await expect(ensureBrowserPairing()).resolves.toBe(true); + + expect(fetchMock).toHaveBeenCalledTimes(4); + expect(window.location.hash).toBe(""); + }); + it("returns false when bootstrap exchange fails", async () => { setTestWindow("http://localhost:3773/#t3_bootstrap=pair-me"); fetchMock.mockResolvedValueOnce( @@ -133,7 +172,7 @@ describe("browserAuth", () => { await expect(ensureBrowserPairing()).resolves.toBe(false); expect(fetchMock).toHaveBeenCalledTimes(1); - expect(window.location.hash).toBe(""); + expect(window.location.hash).toBe("#t3_bootstrap=pair-me"); }); it("checks the existing auth session when no bootstrap token is present", async () => { diff --git a/apps/web/src/browserAuth.ts b/apps/web/src/browserAuth.ts index 1dbc41cdc6..3d819aba27 100644 --- a/apps/web/src/browserAuth.ts +++ b/apps/web/src/browserAuth.ts @@ -1,4 +1,6 @@ const BOOTSTRAP_HASH_KEY = "t3_bootstrap"; +const AUTH_SESSION_POLL_ATTEMPTS = 50; +const AUTH_SESSION_POLL_INTERVAL_MS = 100; interface AuthSessionResponse { readonly authenticated: boolean; @@ -76,6 +78,18 @@ async function fetchSession(serverOrigin: string): Promise { return parsed.authenticated === true; } +async function waitForAuthenticatedSession(serverOrigin: string): Promise { + for (let attempt = 0; attempt < AUTH_SESSION_POLL_ATTEMPTS; attempt += 1) { + if (await fetchSession(serverOrigin)) { + return true; + } + if (attempt < AUTH_SESSION_POLL_ATTEMPTS - 1) { + await new Promise((resolve) => setTimeout(resolve, AUTH_SESSION_POLL_INTERVAL_MS)); + } + } + return false; +} + export async function ensureBrowserPairing(): Promise { if (typeof window === "undefined") return false; if (window.nativeApi) return true; @@ -84,9 +98,10 @@ export async function ensureBrowserPairing(): Promise { const bootstrapToken = consumeBootstrapTokenFromHash(); if (bootstrapToken) { - clearBootstrapTokenFromUrl(); try { await exchangeBootstrapToken(serverOrigin, bootstrapToken); + clearBootstrapTokenFromUrl(); + return waitForAuthenticatedSession(serverOrigin); } catch { return false; } diff --git a/apps/web/src/main.tsx b/apps/web/src/main.tsx index 78f7af1e29..013953452c 100644 --- a/apps/web/src/main.tsx +++ b/apps/web/src/main.tsx @@ -18,6 +18,7 @@ const history = isElectron ? createHashHistory() : createBrowserHistory(); const router = getRouter(history); document.title = APP_DISPLAY_NAME; +const UNPAIRED_RETRY_INTERVAL_MS = 1_000; function PairingRequiredScreen() { return ( @@ -73,6 +74,43 @@ function BootstrappedApp() { }; }, []); + React.useEffect(() => { + if (state !== "unpaired") { + return; + } + + let active = true; + let retryTimeout: ReturnType | null = null; + + const retry = () => { + void ensureBrowserPairing() + .then((authenticated) => { + if (!active || !authenticated) { + if (active) { + retryTimeout = setTimeout(retry, UNPAIRED_RETRY_INTERVAL_MS); + } + return; + } + initializeNativeApi(); + setState("ready"); + }) + .catch(() => { + if (active) { + retryTimeout = setTimeout(retry, UNPAIRED_RETRY_INTERVAL_MS); + } + }); + }; + + retryTimeout = setTimeout(retry, UNPAIRED_RETRY_INTERVAL_MS); + + return () => { + active = false; + if (retryTimeout !== null) { + clearTimeout(retryTimeout); + } + }; + }, [state]); + if (state === "loading") { return (