Purpose
The Better Auth factory configures sessions, Organizations, admin, passkeys, two-factor, Expo and MCP OAuth. It is four modules with one job each: auth-options (origins, credentials, sessions, rate limits, plugins), auth-hooks (what Better Auth runs around its writes and endpoints), auth-effects (the Audit Events, emails and storage deletes a write causes, handed to its unit of work) and auth-handler (createAuth: the instance and its transactional HTTP handler). request-runtime is what the Worker entry hands a request (waitUntil, tracing, the request facts), request-scope memoizes one database and one auth instance per request for whoever opens them, and the package root is the composition root: it exports createRequestScope, built from createDb and createAuth, and nothing else (backend conventions). access builds the access control both role layers use (the admin plugin's platform roles, the organization plugin's built-in and Custom roles) and the pure decisions requirePermission makes; access-policy holds the rules for Custom role writes and their Audit Events (Roles and permissions). Request scope shares the authorized database/auth instance through one HTTP invocation. Outcome modules normalize sign-in and challenge results, and user-agent is the client-safe leaf (parseUserAgent) shared by web and mobile session and activity views. client-ip is the one rule for the client address used by rate limits and audit: cf-connecting-ip on Workers, otherwise TRUSTED_PROXY_HEADER/TRUSTED_PROXIES when a Node deploy configures them, else the socket address. An invalid header name or proxy count is logged and ignored: the socket address is used, never a guessed hop (runtime switches). user-objects lists the storage objects a User owns outside every Organization (their Feedback Report images) and deletes them after the account deletion commits (Feedback reports).
Public exports
| Subpath | Target |
|---|---|
. | ./src/index.ts |
./access | ./src/access.ts |
./access-policy | ./src/access-policy.ts |
./admin-audit | ./src/admin-audit.ts |
./auth-handler | ./src/auth-handler.ts |
./errors | ./src/errors.ts |
./oauth-grant-generation | ./src/oauth-grant-generation.ts |
./rate-limit-storage | ./src/rate-limit-storage.ts |
./request-runtime | ./src/request-runtime.ts |
./request-scope | ./src/request-scope.ts |
./server-session | ./src/server-session.ts |
./session-handoff | ./src/session-handoff.ts |
./session-outcome | ./src/session-outcome.ts |
./two-factor-outcome | ./src/two-factor-outcome.ts |
./user-agent | ./src/user-agent.ts |
These paths come from packages/auth/package.json. A . entry is the package root. Conditional targets distinguish platform or type resolution; consumers should import the subpath, not its source file.
How to extend
Change auth-options (or auth-hooks for a lifecycle hook) for server behavior and the outcome modules for shared interpretations. An effect that follows a write (an email, an Audit Event, a storage delete) is added to auth-effects, so it waits for COMMIT. Test revoked sessions, bans, impersonation and Organization membership. Do not export client constants through a server entry.
Read the identity and request scope guide for the full flow.