auth_gate.ts

The root layout's auth gate: which routes render without a session or a backend, what the gate shows in place of the app, and where a login or bootstrap returns to.

The public routes (about and docs) are plain content, so a static build served without the daemon still renders them — and prerenders them with their content. Every other route needs the daemon: without one the gate says so instead of showing a login form that can't work.

view source

Declarations
#

9 declarations

AuthGateInput
#

auth_gate.ts view source

AuthGateInput import type {AuthGateInput} from '@fuzdev/zzz/auth_gate.js';

session_checked

Whether the first session check has settled — later checks don't show checking.

type boolean

backend_checked

Whether the backend probe has settled.

type boolean

backend_reachable

Whether a zzz backend answered the probe.

type boolean

verified

AuthState.verified.

type boolean

needs_bootstrap

AuthState.needs_bootstrap.

type boolean

AuthGateState
#

auth_gate.ts view source

AuthGateState

type "checking" | "daemon_unreachable" | "bootstrap" | "login"

import type {AuthGateState} from '@fuzdev/zzz/auth_gate.js';

What the gate shows in place of the app:

  • checking — the first session check or the backend probe hasn't settled (or the session just verified and the app is mounting)
  • daemon_unreachable — no zzz backend answers
  • bootstrap — no account exists yet
  • login — the backend is up and there's no session

backend_reachable_context
#

auth_gate.ts view source

{ get: (error_message?: string | undefined) => () => boolean | null; get_maybe: () => (() => boolean | null) | undefined; set: (value: () => boolean | null) => () => boolean | null; } import {backend_reachable_context} from '@fuzdev/zzz/auth_gate.js';

Whether a zzz backend answered the root layout's probe — null until the probe settles (and during SSR). Set by the root layout as a getter, so public routes can say why they're missing the app: no daemon, or no login.

HEALTH_PATH
#

auth_gate.ts view source

"/health" import {HEALTH_PATH} from '@fuzdev/zzz/auth_gate.js';

The zzz backend's liveness route.

is_public_route
#

auth_gate.ts view source

(pathname: string, public_paths?: readonly string[]): boolean import {is_public_route} from '@fuzdev/zzz/auth_gate.js';

Whether pathname is a public route or below one. A .html suffix is ignored: a static server without clean URLs serves the prerendered about.html at /about.html, and SvelteKit hydrates it as /about.

pathname

the URL pathname, e.g. page.url.pathname

type string

public_paths

the public route paths (resolved against the base path)

type readonly string[]
default PUBLIC_ROUTE_PATHS

returns

boolean

probe_backend
#

auth_gate.ts view source

(fetch_health?: () => Promise<Response>): Promise<boolean> import {probe_backend} from '@fuzdev/zzz/auth_gate.js';

Probe whether a zzz backend is serving. Only its health reply counts — a static server (or an SPA fallback answering every path with HTML) is not a backend, and neither is a network error.

fetch_health

fetches the health route (injectable for tests)

type () => Promise<Response>
default () => fetch(HEALTH_PATH)

returns

Promise<boolean>

whether the zzz backend answered

PUBLIC_ROUTE_PATHS
#

auth_gate.ts view source

readonly ["/about", "/docs"] import {PUBLIC_ROUTE_PATHS} from '@fuzdev/zzz/auth_gate.js';

Routes that render without a session or a backend.

to_auth_gate_state
#

auth_gate.ts view source

(input: AuthGateInput): AuthGateState import {to_auth_gate_state} from '@fuzdev/zzz/auth_gate.js';

Derive the gate's state. Only the *first* session check shows checking: a login or bootstrap in flight keeps its form mounted, so a failure leaves the fields as typed.

input

returns

AuthGateState

to_auth_redirect
#

auth_gate.ts view source

(url: Pick<URL, "search" | "pathname" | "hash">, root?: string): string import {to_auth_redirect} from '@fuzdev/zzz/auth_gate.js';

Where a login or bootstrap returns to: the page the gate replaced, with its query and hash, so a deep link survives logging in. A path starting // or /\ would read as protocol-relative — another origin — so it returns to the root instead.

url

the current page URL

type Pick<URL, "pathname" | "search" | "hash">

root

where an unsafe path falls back to (the resolved /)

type string
default '/'

returns

string