Files
t3code/apps/desktop/src/backend/DesktopLocalEnvironmentAuth.ts
T
Josh Gratton bb6fafc16a Merge upstream/main into josh/desktop-wsl-parallel-backends (auth migration)
Brings in upstream's desktop-auth to Clerk-bridge migration (#3092) plus 9 other
commits. Resolution preserves the WSL parallel-backends work while adopting the
new primary-auth flow.

Contracts / IPC: keep our plural getLocalEnvironmentBootstraps; add upstream's
new getLocalEnvironmentBearerToken (primary bearer token) across the bridge,
preload, and the IPC channel / method / handlers.

Desktop:
- DesktopLocalEnvironmentAuth (upstream's new primary-auth service) now reads the
  primary instance from our DesktopBackendPool instead of the removed legacy
  DesktopBackendManager.
- main.ts composes both our WSL backend layer and the new local-environment-auth
  and Clerk layers.
- DesktopWindow adopts upstream's local-client window loading (getDesktopUrl /
  t3:// scheme) rather than loading the backend URL, dropping the now-obsolete
  backend-URL plumbing. The "Connecting to WSL" splash is kept, re-pointed onto
  the existing backend-ready latch (upstream's DesktopState.backendReady addition
  did not land in this merge; the backend pool drives readiness through
  handleBackendReady / handleBackendNotReady).

Web: keep our Layer.effect platform source (primary plus WSL secondaries); the
primary now authenticates via upstream's primaryEnvironmentHttpLayer and the
secondaries via FetchHttpClient.

Verified: typecheck (web, desktop), web tests (connection / provider /
authBootstrap / desktopAuth), desktop tests (DesktopWindow splash,
DesktopLocalEnvironmentAuth), and lint all pass.
2026-06-19 17:17:17 -04:00

89 lines
3.2 KiB
TypeScript

import { bootstrapRemoteBearerSession } from "@t3tools/client-runtime/authorization";
import { PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Ref from "effect/Ref";
import * as Semaphore from "effect/Semaphore";
import { HttpClient } from "effect/unstable/http";
import * as DesktopBackendPool from "./DesktopBackendPool.ts";
export interface DesktopLocalEnvironmentAuthShape {
readonly getBearerToken: Effect.Effect<string, DesktopLocalEnvironmentAuthError>;
}
export class DesktopLocalEnvironmentAuthError extends Data.TaggedError(
"DesktopLocalEnvironmentAuthError",
)<{
readonly message: string;
readonly cause?: unknown;
}> {}
export class DesktopLocalEnvironmentAuth extends Context.Service<
DesktopLocalEnvironmentAuth,
DesktopLocalEnvironmentAuthShape
>()("@t3tools/desktop/backend/DesktopLocalEnvironmentAuth") {}
export const layer = Layer.effect(
DesktopLocalEnvironmentAuth,
Effect.gen(function* () {
const pool = yield* DesktopBackendPool.DesktopBackendPool;
const httpClient = yield* HttpClient.HttpClient;
const tokenRef = yield* Ref.make(Option.none<string>());
const mutex = yield* Semaphore.make(1);
const getBearerToken = mutex
.withPermits(1)(
Effect.gen(function* () {
const cached = yield* Ref.get(tokenRef);
if (Option.isSome(cached)) {
return cached.value;
}
const instances = yield* pool.list;
const primary = instances.find(
(instance) => instance.id === PRIMARY_LOCAL_ENVIRONMENT_ID,
);
const configOption = primary === undefined ? Option.none() : yield* primary.currentConfig;
if (Option.isNone(configOption)) {
return yield* new DesktopLocalEnvironmentAuthError({
message: "Local backend is not configured.",
});
}
const config = configOption.value;
const credential = config.bootstrap.desktopBootstrapToken;
if (!credential) {
return yield* new DesktopLocalEnvironmentAuthError({
message: "Local backend did not issue a bootstrap credential.",
});
}
const session = yield* bootstrapRemoteBearerSession({
httpBaseUrl: config.httpBaseUrl.href,
credential,
clientMetadata: {
label: "T3 Code Desktop",
deviceType: "desktop",
},
}).pipe(
Effect.provideService(HttpClient.HttpClient, httpClient),
Effect.mapError(
(cause) =>
new DesktopLocalEnvironmentAuthError({
message: "Failed to create the local desktop bearer session.",
cause,
}),
),
);
yield* Ref.set(tokenRef, Option.some(session.access_token));
return session.access_token;
}),
)
.pipe(Effect.withSpan("desktop.localEnvironmentAuth.getBearerToken"));
return DesktopLocalEnvironmentAuth.of({ getBearerToken });
}),
);