Authentication
Email codes and magic links
Email sign-in has three steps: bind the request, send a proof, then submit its reference and secret to complete sign-in.
This guide shows local service calls inside existing Effect request handlers.
Provide AppAuth and the HTTP request boundary. For browser access, declare the
actions in a shared contract.
Enable email codes
Section titled “Enable email codes”import { Schema } from "effect";import { Auth, Email, Sessions } from "@yielded/auth";
export const AppAuth = Auth.make("app/Auth", { claims: Schema.Struct({ displayName: Schema.String }), sessions: Sessions.stateful(), strategies: { email: Email.makeCode(), }, defaultStrategy: "email",});Codes default to six digits and five minutes. Override digits or policy when
needed. Supply shared ProofKeys through your key configuration.
Start the flow and send a code
Section titled “Start the flow and send a code”const auth = yield* AppAuth;const started = yield* auth.beginSignIn({ flowId });The next request sends the code:
const auth = yield* AppAuth;const sent = yield* auth.signIn({ flowId, requestId, requestBinding, email, returnTarget: "/account", locale: "en",});beginSignIn delivers a private request-binding credential. The next request uses
that credential as requestBinding. signIn returns a proof reference, not the code.
Auth renders the email; your EmailDelivery service sends the finished message.
requestBinding is a private server input. Map it to the request-binding slot
through the contract’s requestFields; named client methods then omit it.
Never make the browser read an HttpOnly cookie or send it as JSON.
Complete sign-in
Section titled “Complete sign-in”const auth = yield* AppAuth;const result = yield* auth.completeSignIn({ flowId, requestBinding, email, reference, secret: code, returnTarget: "/account",});Check result.completion._tag before granting access. Only Authenticated
contains a session. The proof is consumed before independent session issuance; if
issuance fails or its outcome is unknown, start a new flow. The HTTP adapter maps private credentials
to cookies so they stay out of ordinary browser payloads.
Use a magic link instead
Section titled “Use a magic link instead”import { Email } from "@yielded/auth";
export const magicLink = Email.makeLink({ url: "https://app.example.com/sign-in" });Use this strategy with the same begin → request → complete flow.
Auth places the reference and secret in the URL fragment. Use
EmailDelivery.parseLinkFragment to read it. A landing-page GET
must not consume it: show a confirmation action, clear the fragment from browser
history, and complete from the originating client.
Proof expiry and rate limits
import { type Proofs } from "@yielded/auth";
export const proofPolicy: Proofs.ProofPolicy = { lifetimeMillis: 5 * 60_000, maximumFailedAttempts: 5, abuse: { issues: { limit: 5, windowMillis: 60 * 60_000 }, attempts: { limit: 10, windowMillis: 5 * 60_000 }, subjectIssues: { limit: 5, windowMillis: 60 * 60_000 }, subjectAttempts: { limit: 10, windowMillis: 5 * 60_000 }, actionIssues: { limit: 1000, windowMillis: 60 * 60_000 }, actionAttempts: { limit: 1000, windowMillis: 5 * 60_000 }, resendCooldownMillis: 30_000, },};Pass this policy to Email.makeCode({ policy: proofPolicy }). Keep
resendCooldownMillis shorter than the proof lifetime. Rate limits apply to
repeated requests and unknown addresses too. The default limiter is process-local;
share identifier and subject limits across
replicas or per-request runtimes. Action budgets remain per instance.
To resend, keep the original flow and private request binding, then request a new
code after the cooldown. A different binding cannot replace an unexpired proof,
even with its public reference. requestId is correlation only; it does not
recover an earlier receipt or delivery.
Supply the services
Section titled “Supply the services”For SQL, AuthPersistence.make(AppAuth) supplies verified-email lookup, proof
storage, and session authority through managed or mapped tables.
Your application supplies claims, delivery, keys, and
crypto Layers. The return-target allowlist
restricts where sign-in can send the user:
import { Layer } from "effect";import { Auth, Email, Proofs } from "@yielded/auth";
import { AppAuth } from "./auth";import { requestBinding, proofKeys } from "./auth-config";import { resolveEmailClaims } from "./auth-accounts";import { Persistence, storage } from "./auth-persistence";import { CryptoLive } from "./crypto-live";import { DatabaseLive } from "./database";import { EmailLive } from "./email";
export const EmailServicesLive = Layer.mergeAll( Persistence.layer.pipe(Layer.provide(Persistence.Config.layer(storage))), Layer.succeed(AppAuth.strategies.email.SessionClaims, { resolve: resolveEmailClaims }), Email.EmailReturnTargets.exactRoutes(["/account"]), EmailLive,);
export const AuthLive = AppAuth.layer.pipe( Layer.provide(EmailServicesLive), Layer.provide(Auth.RequestBindingConfig.layer(requestBinding)), Layer.provide(Proofs.ProofKeys.layer(proofKeys)), Layer.provide(DatabaseLive), Layer.provide(CryptoLive),);For sibling apps, configure cross-origin return targets.
The relative imports are your application modules. DatabaseLive supplies the
Effect SQL client; apply the storage migrations
before starting Auth. EmailLive implements
email delivery; build Auth in a scope that outlives requests
so its delivery worker can finish.
Sign-in uses existing, verified email identities; add registration
to create accounts. For custom persistence, supply EmailSignInTargets and
ProofPersistence explicitly; see the email adapter.
HTTP derives the network key for request admission from the socket peer. See HTTP admission for proxy configuration and overrides.
Register a mailbox owner
Section titled “Register a mailbox owner”Compose Email.makeRegistration({ namespace, registration: Registration }) with
Email.makeCode({ namespace }) using the same namespace. A guest calls
beginRegistration → register → completeRegistration. The
login contract
and server
show both strategies and their private request-binding cookie.
Provide RegistrationAuthority with your Drizzle adapter’s
makeEmailRegistrationServices, using the same proof mapping as sign-in.
AuthPersistence.layer supplies shared proof storage but leaves registration
authority explicit. For fully explicit composition, also provide
makeProofPersistenceServices. Provisioning is synchronous and must be idempotent
by the authority’s stable requestId.
When your policy allows it, mailbox proof can reclaim an unverified email reservation for a new account. It never transfers the earlier account’s data or credentials, or replaces verified ownership. See email persistence for mapping and invalidation requirements.
Registration does not issue a session. Start a fresh email sign-in afterward;
an authenticated user can then call addPassword when password management is enabled.
Use Email.makeAddresses for authenticated address management. Confirming an
address there does not create or link an account.
See the combined login example to share Auth, sessions, and client methods with GitHub.