Retry browser pairing until auth cookie is visible

- Poll auth session after bootstrap exchange before declaring pairing complete
- Keep the bootstrap hash on failed exchange and retry unpaired startup in the UI
This commit is contained in:
Theo Browne committed 2026-03-27 00:24:22 -07:00
1 parent e980a2fd8f
commit 12ecd648ba
3 files changed
+94 -2

No files matched your search

+40 -1
View File
@@ -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 () => {
+16 -1
View File
@@ -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<boolean> {
return parsed.authenticated === true;
}
async function waitForAuthenticatedSession(serverOrigin: string): Promise<boolean> {
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<boolean> {
if (typeof window === "undefined") return false;
if (window.nativeApi) return true;
@@ -84,9 +98,10 @@ export async function ensureBrowserPairing(): Promise<boolean> {
const bootstrapToken = consumeBootstrapTokenFromHash();
if (bootstrapToken) {
clearBootstrapTokenFromUrl();
try {
await exchangeBootstrapToken(serverOrigin, bootstrapToken);
clearBootstrapTokenFromUrl();
return waitForAuthenticatedSession(serverOrigin);
} catch {
return false;
}
+38
View File
@@ -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<typeof setTimeout> | 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 (
<div className="flex h-screen flex-col bg-background text-foreground">