Auth / foldkit-auth

auth

When your application uses Yielded Auth for sign-in, Yielded sets an HTTP session cookie — but your data endpoints (foldkit-remote-server's serveFetch), real-time sync sockets (foldkit-sync's defineDocumentHost), and server-rendered pages (foldkit-ssr) each need to know who is asking. Re-implementing cookie parsing, Origin and CSRF header checks, and verifySession lookups on every endpoint risks letting a cross-site request borrow someone's session or silently treating an ended session as an anonymous visitor.

foldkit-auth turns a Yielded Auth session cookie into a typed principal for Remote, Sync, and SSR. AuthServer.resolver verifies the request's origin, checks the session against your Auth service, and maps it to your application's user type — or returns Option.none() for a visitor, or 401 when a session ended or came from an untrusted origin. AuthServer.host pairs the scripted, navigation, and auth route handlers from one configuration.

See it running end-to-end in examples/auth, examples/reference, and examples/cloudflare.

Install

sh
pnpm add effect @yielded/auth foldkit-remote foldkit-remote-server foldkit-auth

@yielded/auth is a peer dependency while Yielded is in beta. Run npx --legacy-peer-deps foldkit-plus setup in your project directory to print the pinned package version and effect overrides for your package manager.

A first protected endpoint

This configures a Yielded Auth service, builds a principal resolver with AuthServer.resolver, and wires it into serveFetch so every /remote request receives Option.some({ id }) or Option.none():

ts
import { Auth, AuthContract, Password, Sessions } from '@yielded/auth'
import { Context, Effect, Layer, Option, Schema } from 'effect'
import { AuthServer } from 'foldkit-auth/server'
import { RemoteServer } from 'foldkit-remote-server'
import { serveFetch } from 'foldkit-remote-server/fetch'

const AppAuth = Auth.make(
  AuthContract.make('readme/Auth', {
    claims: Schema.Struct({}),
    actions: sessions => ({ signIn: AuthContract.passwordSignIn(sessions) }),
  }),
  {
    sessions: Sessions.stateful({ idleTimeout: '10 minutes', maxAge: '1 hour' }),
    strategies: { password: Password.make() },
    defaultStrategy: 'password',
  },
)
const server = RemoteServer.make<Option.Option<{ readonly id: string }>>({ entities: [] })

export const makeRemote = (layer: () => Layer.Layer<Context.Service.Identifier<typeof AppAuth>>) =>
  serveFetch({
    server,
    resolvePrincipal: AuthServer.resolver({
      auth: AppAuth,
      http: { origin: 'https://app.example' },
      principal: session => Effect.succeed({ id: session.subjectId }),
    }),
    layer,
  })

A request with no session cookie passes Option.none() (a visitor) into server. A request carrying a valid session cookie with Origin: https://app.example and x-effect-auth-csrf: 1 resolves to Option.some({ id: session.subjectId }). Any request carrying a cookie from an untrusted origin, missing the CSRF header, or holding an expired session returns 401 (RemoteUnauthenticated).

  • AuthServer.resolver(config) declares the per-request principal Effect. It validates http.origin at creation time and reads headers and the session store on each call.
  • auth is your Auth.make(…) service; serveFetch's layer provides the same persistence layer shared with Yielded's sign-in routes.
  • http is the same object passed to Yielded's Http.make(AppAuth, http), so cookie names, trusted origins, and CSRF headers stay aligned.
  • principal(session) maps a verified session to your application user per request. Read fresh memberships here from session.subjectId rather than relying on claims captured at sign-in; you can also fail with RemoteServer.unauthenticated(reason).