ui/auth_state.svelte.ts

Reactive state for cookie-based authentication.

SPA auth pattern: prerendered static HTML served by Hono, no SvelteKit server for SSR sessions. On load, fetches GET /api/account/status which returns the current account (200) or 401 with optional bootstrap_available. Login sends username + password once, then a signed httpOnly cookie handles all subsequent requests.

<script lang="ts"> import {AuthState, auth_state_context} from '@fuzdev/fuz_app/ui/auth_state.svelte.ts'; const auth = new AuthState(); auth_state_context.set(auth); auth.check_session(); </script> {#if auth.verifying} <p>checking session…</p> {:else if auth.needs_bootstrap} <BootstrapForm /> {:else if !auth.verified} <LoginForm /> {:else} <p>logged in as {auth.account?.username}</p> <button onclick={() => auth.logout()}>logout</button> {/if}
view source

Declarations
#

3 declarations

auth_state_context
#

ui/auth_state.svelte.ts view source

{ get: (error_message?: string | undefined) => AuthState; get_maybe: () => AuthState | undefined; set: (value: AuthState) => AuthState; } import {auth_state_context} from '@fuzdev/fuz_app/ui/auth_state.svelte.js';

Svelte context for AuthState. Use auth_state_context.set(state) in the provider and auth_state_context.get() to access.

AuthState
#

ui/auth_state.svelte.ts view source

import {AuthState} from '@fuzdev/fuz_app/ui/auth_state.svelte.js';

verifying

type boolean

$state.raw

verified

type boolean

$state.raw

verify_error

type string | null

$state.raw

account

type SessionAccount | null

$state.raw

actor

type ActorSummaryJson | null

$state.raw

role_grants

type RoleGrantSummaryJson[]

$state.raw

active_role_grants

The live subset of role_grants, at every scope. Liveness is judged when role_grants is assigned, so a grant that expires while the page stays open reads as active until the next check_session.

type RoleGrantSummaryJson[]

readonly $derived

global_roles

The roles held globally — the role of each active grant whose scope_id is null. A grant scoped to one resource is not in here: it confers its role on that resource only, and a scoped grant of a builtin role confers nothing, so a role gate reads the global grant. Scoped grants are read from active_role_grants, by scope_id.

Shapes the UI only — the server re-checks every request.

type ReadonlySet<string>

readonly $derived

is_admin

True for an active global admin grant, the rule the server's admin gates apply.

type boolean

readonly $derived

needs_bootstrap

True when bootstrap is available (no accounts exist yet).

type boolean

$state.raw

has_global_role

Whether the account holds an active global grant of role — reactive when read in a $derived or the template. A scoped grant doesn't count; see global_roles.

type (role: string): boolean

role

the role to check

type string
returns boolean

check_session

Check auth state and bootstrap availability.

Fetches GET /api/account/status — returns account info (200) or 401 with optional bootstrap_available flag. Called on init, and after login/bootstrap to refresh state.

type (): Promise<void>

returns Promise<void>

mutates

  • this

login

Log in with username and password. Translates 401 / 429 to friendly messages on verify_error; refreshes the session via check_session on success.

type (username: string, password: string): Promise<boolean>

username

type string

password

type string
returns Promise<boolean>

true if login succeeded, false otherwise

mutates

  • this

bootstrap

Bootstrap the first keeper account using a single-use token.

type (token: string, username: string, password: string): Promise<boolean>

token

type string

username

type string

password

type string
returns Promise<boolean>

true if bootstrap succeeded, false otherwise

mutates

  • this

signup

Sign up via invite (or open signup, when app_settings.open_signup is true server-side). Translates 403 / 409 / 429 to friendly messages on verify_error.

type (username: string, password: string, email?: string | undefined): Promise<boolean>

username

type string

password

type string

email?

type string
optional
returns Promise<boolean>

true if signup succeeded, false otherwise

mutates

  • this

logout

Log out — best-effort POST /api/account/logout to clear the session cookie, then clears local state regardless of the network outcome.

type (): Promise<void>

returns Promise<void>

mutates

  • this

format_rate_limit_message
#

ui/auth_state.svelte.ts view source

(retry_after: unknown): string import {format_rate_limit_message} from '@fuzdev/fuz_app/ui/auth_state.svelte.js';

The message a 429 shows, from its retry_after (seconds).

A short wait reads as one — the spines answer retry_after: 1 when the cap is held only by attempts still in flight — rather than rounding up to a minute; a missing value reads as a minute.

retry_after

the response's retry_after, when it carried one

type unknown

returns

string

Depends on
#

Imported by
#