Auth / foldkit-auth/server

AuthServer

namespace · packages/auth/src/server.ts:295

const AuthServer = { host, resolver, answered }

In the guides

Members

AuthServer.host

function · packages/auth/src/server.ts:252

host<
  I,
  S extends Sessions.SessionMetadata,
  Api extends Auth.SessionApi<S, unknown>,
  Actions extends AuthContract.AuthActions,
  User,
  R = never,
>(
  config: HostConfig<I, S, Api, Actions, User, R>,
): AuthHost<User, R>

One Yielded application as a host serves it: its routes, and the two resolvers that read its cookie, typed apart so each fits only its own consumer. scripted is serveFetch's (/remote, which writes): it reads the cookie only from a request that carries a trusted Origin and the CSRF header. navigation is pageReader's (a page render, which only reads): it reads the cookie of a GET or HEAD the browser navigates to, which carries neither, and refuses any other method, an upgrade, and a script's fetch. Handing either to the other's consumer is a type error. Both read the principal per request; services is built once and holds none. Throws when http names an origin that is not one, or carries oauth.

ts
const host = AuthServer.host({ auth: AppAuth, http, services, principal, key: Policy.key })
const remote = serveFetch({ server, layer, ...host.scripted })
const read = pageReader({ server, layer, ...host.navigation })

AuthServer.resolver

function · packages/auth/src/server.ts:127

resolver<I, S extends VerifiedSession, User, R = never>(
  config: ResolverConfig<I, S, User, R>,
): Resolver<User, I | R>

Who a request is, from its Yielded session cookie, for a host's resolvePrincipal. No cookie is a visitor (Option.none()). A cookie is used only from a request that shows it came from the application, as Yielded's own mutations must: an Origin it trusts and, for requests: 'scripted', its CSRF header. From any other it is refused, as is a cookie that no longer verifies, two of them, or an x-foldkit-subject naming someone else. A session store that fails is a defect, never a visitor. Throws when http names an origin that is not one.

ts
serveFetch({
  server,
  resolvePrincipal: AuthServer.resolver({
    auth: AppAuth,
    http, // the options given Http.make(AppAuth, http)
    principal: session => Users.find(session.subjectId),
  }),
  layer: env => AuthLive(env),
})

AuthServer.answered

function · packages/auth/src/server.ts:356

answered<User>(config: {
  readonly key: (user: User) => string
}): (principal: Option.Option<User>) => Readonly<Record<string, string>>

The header saying whom an answer was for, for serveFetch's headers: what believing's fetch repeats on the client's next request, so the resolver refuses it once the cookie means someone else. Takes the resolver's own config, whose key it requires, so the two name the principal alike.

ts
const auth = { auth: AppAuth, http, principal, key: Policy.key }
serveFetch({
  server,
  resolvePrincipal: AuthServer.resolver(auth),
  headers: AuthServer.answered(auth),
  layer: env => AuthLive(env),
})