Handle.ts overview

The SvelteKit handle hook: resolves the session once through the core’s SessionResolution, before any load, puts it on event.locals.auth, and only after rendering sets the cache headers, forcing private, no-store for a signed-in viewer. The app supplies the lookup.


Exports Grouped by Category


hooks

handle

Build the handle hook.

It resolves the session, sets event.locals.auth (LocalsAuth), renders the route, and forces Cache-Control: private, no-store on the response when a viewer is signed in or the lookup sent Set-Cookie lines, which it appends to the response (never for an API-key or bearer caller). Compose it first in sequence(...), so every later hook and every load sees the resolved viewer.

Example

import { strictEqual } from "node:assert"
import { Effect } from "effect"
import * as Option from "effect/Option"
import { handle } from "@thomasfosterau/effect-auth-sveltekit/Handle"

const hook = handle({
  lookup: () => Effect.succeed(Option.none()),
  failure: { mode: "closed" }
})

const event = {
  request: new Request("https://app.example/"),
  getClientAddress: () => "203.0.113.9",
  cookies: { get: () => undefined },
  locals: {}
}
const response = await hook({ event, resolve: () => new Response("ok") })
strictEqual(await response.text(), "ok")

Signature

declare const handle: <E>(
  options: HandleOptions<E>
) => <Event extends HandleEvent>(input: HandleInput<Event>) => Promise<Response>

Source

Since v0.1.0

models

FailurePolicy (type alias)

What a session-lookup fault does to the request.

  • closed: the fault fails the request.
  • open: the request continues as anonymous, and onDegraded hears about it. Only safe when anonymous is the lower-privilege outcome (see SessionResolution.resolveFailOpen).

Signature

type FailurePolicy<E = unknown> =
  | { readonly mode: "closed" }
  | {
      readonly mode: "open"
      readonly onDegraded: (cause: Cause.Cause<E>) => Effect.Effect<void>
    }

Source

Since v0.1.0

HandleEvent (interface)

The part of SvelteKit’s RequestEvent the hook reads and writes. @sveltejs/kit’s RequestEvent satisfies it, so this package needs no dependency on SvelteKit itself.

Signature

export interface HandleEvent {
  readonly request: Request
  /** The client's address as the SvelteKit adapter knows it, for the lookup. */
  readonly getClientAddress: () => string
  readonly cookies: { readonly get: (name: string) => string | undefined }
  readonly locals: object
}

Source

Since v0.1.0

HandleInput (interface)

The handle input, structurally, as SvelteKit’s Handle type passes it.

Signature

export interface HandleInput<Event extends HandleEvent> {
  readonly event: Event
  readonly resolve: (event: Event) => Response | Promise<Response>
}

Source

Since v0.1.0

HandleOptions (interface)

Options for handle.

Signature

export interface HandleOptions<E = unknown> {
  /** The session cookie's naming, as the auth handler was configured. */
  readonly cookies?: SessionCookies.SessionCookieNaming
  /**
   * Look the session up for this request: `Option.none()` when nobody is
   * signed in, or a {@link SessionLookup} carrying `Set-Cookie` lines to
   * replay. Runs only when the request carries a credential. Over
   * `Http.RemoteSession`, pass the client address on:
   * `(request, event) => auth.read(request, { clientAddress: event.getClientAddress() })`.
   */
  readonly lookup: (
    request: Request,
    event: HandleEvent
  ) => Effect.Effect<Option.Option<SessionWithUser> | SessionLookup, E>
  /** What a lookup fault does. Required: neither direction is a safe default. */
  readonly failure: FailurePolicy<E>
}

Source

Since v0.1.0

LocalsAuth (interface)

What the hook puts on event.locals.auth. Declare it in App.Locals:

declare global {
  namespace App {
    interface Locals {
      auth: import("@thomasfosterau/effect-auth-sveltekit").LocalsAuth
    }
  }
}

Signature

export interface LocalsAuth {
  /** The signed-in viewer's session and user, or `null` for anonymous. */
  readonly session: SessionWithUser | null
  /** How resolution went: `skipped`, `resolved`, `anonymous` or `degraded`. */
  readonly outcome: SessionResolution.AuthOutcome
}

Source

Since v0.1.0

SessionLookup (interface)

A lookup’s answer when it carries Set-Cookie lines as well as the session: what Http.RemoteSession’s read answers. The hook replays the lines on the response, so a refreshed cache cookie or the clearing of a stale one reaches the browser.

Signature

export interface SessionLookup {
  readonly session: Option.Option<SessionWithUser>
  readonly setCookie: ReadonlyArray<string>
}

Source

Since v0.4.0

utils

PRIVATE_CACHE_CONTROL

The Cache-Control a response rendered for a signed-in viewer is forced to.

Signature

declare const PRIVATE_CACHE_CONTROL: "private, no-store"

Source


This site uses Just the Docs, a documentation theme for Jekyll.