Skip to content

Build your application

Sessions

Configure sessions on Auth.make. The HTTP boundary supplies incoming credentials and delivers cookies for request-aware methods.

apps/server/auth.ts
import { Schema } from "effect";
import { Auth, Sessions } from "@yielded/auth";
export const AppAuth = Auth.make("app/Auth", {
claims: Schema.Struct({ displayName: Schema.String }),
sessions: Sessions.stateful({
idleTimeout: "30 minutes",
maxAge: "7 days",
renewAfter: "5 minutes",
}),
});

Supply the bound session persistence services through your storage Layer. Adding an authentication strategy also requires your account Layer’s AuthenticationAuthority to approve session issuance.

Mode Verification Revocation visibility
stateful() Persistence, or an opt-in cookie snapshot Immediate without caching; otherwise up to cacheFor
stateless() Signed token At token expiry
stateAssisted() Signed token and authoritative validity storage Immediate

State-assisted validity must share the authority that commits identity changes. Signed claims remain snapshots; checking validity does not refresh profile data. See lifetime defaults and options.

Signed sessions and cookie caching get default signing keys from AUTH_SECRET through Effect Config. Set a stable secret generated from at least 32 random bytes, encoded as hex or base64, and share it across instances. Uncached stateful sessions do not need it. Supply crypto capabilities with your platform Layer.

To supply the secret yourself, provide Auth.AuthConfig at the root:

import { Layer } from "effect";
const AuthLive = AppAuth.layer.pipe(
Layer.provide(Auth.AuthConfig.layer({ secret: authSecret })), // Redacted<string>
);

For validation and custom key rotation, see signing keys.

Caching is off by default. Opt in for stateful sessions:

Sessions.stateful({ cacheFor: "1 minute" });

A valid cookie snapshot avoids a persistence read. It contains public claims and is signed, not encrypted. Revocation, password changes, disablement, and claim updates may remain unseen for up to cacheFor.

The limit is the shorter of five minutes and renewAfter. Features configured with requireImmediateInvalidation: true reject caching when they require server invalidation. Management, step-up, and mutations use authoritative state, not cookie snapshots. Mutation responses clear the browser’s snapshot; other clients and lost responses retain the bounded delay. See cookie behavior.

Inside an Effect handler covered by http.middleware:

const auth = yield* AppAuth;
const session = yield* auth.getSession(); // Allows a cached snapshot
const current = yield* auth.getSession({ fresh: true }); // Reads authoritative state
const required = yield* auth.requireSession; // Fails when anonymous

Missing, invalid, or expired credentials return null; storage failures remain failures. requireSession allows caching and fails with AuthenticationRequired when anonymous. Reads do not renew credentials. A handler that already resolved an authoritative caller reuses that same private source for ordinary session reads; { fresh: true } requests a new inspection. Cookie snapshots never become private authentication evidence.

The Effect client accepts the same options: client.auth.getSession({ fresh: true }). See the client guide for Atom usage. Outside a request, provide the same long-lived Auth Layer and call auth.verifySession(redactedCredential). It verifies through the configured strategy without AuthRequest, cookie snapshots, renewal, or credential delivery. Http.layer provides Auth to its own routes; other runtimes and raw handlers need explicit service provisioning. See non-HTTP verification for the runtime and request-level composition.

const result = yield* auth.signOut;

Sign-out clears the client credential and reports server invalidation separately. Its result distinguishes revoked, already-invalid, client-only, and SessionSignOutUnavailable. Local clearing alone does not prove server revocation.

const session = yield* auth.renewSession;

Renewal delivers a replacement credential through the request boundary. For Electron and iOS, browser sign-in establishes a distinct native session after hosted authentication and account confirmation.

  1. Yielded Auth

    Verify the password, passkey, or provider

  2. Your code

    Approve the current account and credential revision

  3. Your code

    Commit the session

  4. Yielded Auth

    Deliver the cookie

  5. Browser

    Read the public session

Omit sessions when supplying your own session/completion Layers; see custom composition.

Configure completionLayer({ pendingLifetimeMillis, attemptLimit }) with PendingAuthentication persistence to support a second factor. A pending proof is not an authenticated session. TOTP shows the complete Layer setup.

For action-specific authorization, call AppAuth.sessions.inspectInvocation(invocation, credential) to obtain private evidence from the verification that admitted that action. Public session data and cached claims are not substitutes. The mutation’s persistence owner still checks current authority before commit.

Step-up can authorize a protected change, such as a password change or account link, under your policy. It refreshes authenticatedAt without extending the original absolute expiry. Each factor retains its own verification time: a recent step-up does not make every retained factor fresh.