Auth / foldkit-auth

One host: AuthServer.host

A server answering Yielded's auth routes, /remote, and SSR page reads needs three configurations over one Auth service. AuthServer.host builds all three from one config and types scripted and navigation separately so they cannot be swapped by mistake:

ts
const host = AuthServer.host({
  auth: AppAuth,
  http,
  services, // AppAuth's layer, built once: ManagedRuntime.make(AppAuthLive).context()
  principal,
  key: Policy.key,
})

export const site = {
  auth: host.routes, // Yielded's routes under /auth/
  remote: serveFetch({ server, layer: () => Layer.empty, ...host.scripted }),
  read: pageReader({ server, layer: () => Layer.empty, ...host.navigation }),
}
  • host.scripted requires a trusted Origin and x-effect-auth-csrf: 1 and attaches AuthServer.answered headers when key is set. Typed as ScriptedRequest, so passing it to pageReader is a compile error.
  • host.navigation allows GET/HEAD navigations and refuses writes, upgrades, and script fetches. Typed as NavigationRequest, so passing it to serveFetch or defineDocumentHost is a compile error.
  • services provides Auth automatically inside both host.scripted and host.navigation; each layer callback only supplies what your Sources and principal need. Build services once per isolate (never per request).
  • Routes: host.routes mounts Http.make(auth, http) (without OAuth callbacks; passing http.oauth throws). Sync sockets still take AuthServer.resolver({ requests: 'browser', … }). Call host.dispose() when shutting down; services stays caller-managed.

See examples/reference/src/worker.ts on Workers and examples/auth/src/server.ts on Node.