Skip to content

Commit cc43e33

Browse files
fix(web): use environment-specific fallback auth metadata
When the initial session fetch fails, bootstrapServerAuth now advertises desktop-managed-local only if the desktop bridge is present. Browser clients get loopback-browser on loopback hosts and remote-reachable otherwise, both with one-time-token, so PairingRouteSurface does not send web users down the desktop pairing path.
1 parent 4f15f06 commit cc43e33

2 files changed

Lines changed: 90 additions & 9 deletions

File tree

‎apps/web/src/authBootstrap.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ const DESKTOP_AUTH = {
3535
sessionCookieName: "t3_session",
3636
} as const;
3737

38+
const REMOTE_AUTH = {
39+
policy: "remote-reachable",
40+
bootstrapMethods: ["one-time-token"],
41+
sessionMethods: ["browser-session-cookie"],
42+
sessionCookieName: "t3_session",
43+
} as const;
44+
3845
const SESSION_EXPIRES_AT = DateTime.makeUnsafe("2026-04-05T00:00:00.000Z");
3946
const unauthenticatedSession = (auth: AuthSessionState["auth"]): AuthSessionState => ({
4047
authenticated: false,
@@ -286,8 +293,44 @@ describe("resolveInitialServerAuthGateState", () => {
286293
// (status 500 for a non-HTTP cause). The important behavior is that the gate
287294
// resolves to requires-auth instead of rejecting — before the fix this
288295
// rejection crashed TanStack Router's beforeLoad.
289-
await expect(resolveInitialServerAuthGateState()).resolves.toMatchObject({
296+
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
297+
status: "requires-auth",
298+
auth: DESKTOP_AUTH,
299+
errorMessage: "Primary environment request failed during fetch-session-state (HTTP 500).",
300+
});
301+
});
302+
303+
it("uses loopback-browser fallback metadata when the session fetch fails without a desktop bridge", async () => {
304+
const runner: PrimaryHttpEffectRunner = async () => {
305+
throw new Error("session fetch failed");
306+
};
307+
__setPrimaryHttpRunnerForTests(runner);
308+
installTestBrowser("http://localhost/");
309+
310+
const { resolveInitialServerAuthGateState } = await import("./environments/primary");
311+
312+
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
313+
status: "requires-auth",
314+
auth: LOOPBACK_AUTH,
315+
errorMessage: "Primary environment request failed during fetch-session-state (HTTP 500).",
316+
});
317+
});
318+
319+
it("uses remote-reachable fallback metadata when the session fetch fails on a remote origin", async () => {
320+
const runner: PrimaryHttpEffectRunner = async () => {
321+
throw new Error("session fetch failed");
322+
};
323+
__setPrimaryHttpRunnerForTests(runner);
324+
vi.stubEnv("VITE_HTTP_URL", "https://remote.example.com");
325+
vi.stubEnv("VITE_WS_URL", "wss://remote.example.com");
326+
installTestBrowser("https://remote.example.com/");
327+
328+
const { resolveInitialServerAuthGateState } = await import("./environments/primary");
329+
330+
await expect(resolveInitialServerAuthGateState()).resolves.toEqual({
290331
status: "requires-auth",
332+
auth: REMOTE_AUTH,
333+
errorMessage: "Primary environment request failed during fetch-session-state (HTTP 500).",
291334
});
292335
});
293336

‎apps/web/src/environments/primary/auth.ts‎

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import {
1818
stripPairingTokenFromUrl as stripPairingTokenUrl,
1919
} from "../../pairingUrl";
2020

21-
import { PrimaryEnvironmentHttpClient } from "./httpClient";
2221
import { runPrimaryHttp } from "../../lib/runtime";
2322

23+
import { PrimaryEnvironmentHttpClient } from "./httpClient";
24+
import { isLoopbackHostname, readPrimaryEnvironmentTarget } from "./target";
25+
2426
const PrimaryEnvironmentRequestOperation = Schema.Literals([
2527
"fetch-session-state",
2628
"exchange-bootstrap-credential",
@@ -307,6 +309,48 @@ function isTransientBootstrapError(error: unknown): boolean {
307309
return error instanceof DOMException && error.name === "AbortError";
308310
}
309311

312+
const FALLBACK_SESSION_COOKIE_NAME = "t3_session";
313+
314+
function fallbackAuthDescriptor(): AuthSessionState["auth"] {
315+
// The session endpoint is the source of truth for auth metadata. When it is
316+
// unreachable we still need a descriptor so PairingRouteSurface can render
317+
// the right pairing copy. Desktop has a bridge; browsers do not, so they
318+
// must not be sent down the desktop-bootstrap path.
319+
if (window.desktopBridge !== undefined) {
320+
return {
321+
policy: "desktop-managed-local",
322+
bootstrapMethods: ["desktop-bootstrap"],
323+
sessionMethods: ["browser-session-cookie"],
324+
sessionCookieName: FALLBACK_SESSION_COOKIE_NAME,
325+
};
326+
}
327+
328+
const hostname = readFallbackAuthHostname();
329+
const isRemote = hostname === null || !isLoopbackHostname(hostname);
330+
return {
331+
policy: isRemote ? "remote-reachable" : "loopback-browser",
332+
bootstrapMethods: ["one-time-token"],
333+
sessionMethods: ["browser-session-cookie"],
334+
sessionCookieName: FALLBACK_SESSION_COOKIE_NAME,
335+
};
336+
}
337+
338+
function readFallbackAuthHostname(): string | null {
339+
try {
340+
const target = readPrimaryEnvironmentTarget();
341+
if (target) {
342+
return new URL(target.target.httpBaseUrl).hostname;
343+
}
344+
} catch {
345+
// Fall through to the page origin if the primary target is unreadable.
346+
}
347+
try {
348+
return new URL(window.location.href).hostname;
349+
} catch {
350+
return null;
351+
}
352+
}
353+
310354
async function bootstrapServerAuth(urlCredential: string | null): Promise<ServerAuthGateState> {
311355
// The initial session fetch goes through the desktop bearer-token path
312356
// (withPrimaryBearerToken in httpLayer.ts), which calls the main-process
@@ -320,15 +364,9 @@ async function bootstrapServerAuth(urlCredential: string | null): Promise<Server
320364
try {
321365
currentSession = await fetchSessionState();
322366
} catch (error) {
323-
const fallbackAuth: AuthSessionState["auth"] = {
324-
policy: "desktop-managed-local",
325-
bootstrapMethods: ["desktop-bootstrap"],
326-
sessionMethods: ["browser-session-cookie"],
327-
sessionCookieName: "t3_session",
328-
};
329367
return {
330368
status: "requires-auth",
331-
auth: fallbackAuth,
369+
auth: fallbackAuthDescriptor(),
332370
errorMessage: error instanceof Error ? error.message : "Authentication failed.",
333371
};
334372
}

0 commit comments

Comments
 (0)