@@ -18,9 +18,11 @@ import {
1818 stripPairingTokenFromUrl as stripPairingTokenUrl ,
1919} from "../../pairingUrl" ;
2020
21- import { PrimaryEnvironmentHttpClient } from "./httpClient" ;
2221import { runPrimaryHttp } from "../../lib/runtime" ;
2322
23+ import { PrimaryEnvironmentHttpClient } from "./httpClient" ;
24+ import { isLoopbackHostname , readPrimaryEnvironmentTarget } from "./target" ;
25+
2426const 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+
310354async 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