# better-auth/better-auth architecture

> Architecture diagram of the GitHub repository better-auth/better-auth, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/better-auth/better-auth
- Repository: https://github.com/better-auth/better-auth
- Video tour (about a minute): https://gitdiagram.com/better-auth/better-auth/video

## Overview

Better Auth is a TypeScript authentication and authorization framework. Its central workflow accepts an application request, dispatches it to authentication endpoints, resolves or changes identity and session state, and returns a response with session cookies. The repository also includes client SDKs, storage adapters, and feature plugins for distinct authentication and authorization flows. The map emphasizes these runtime capabilities; some plugin and adapter connections are based on documented framework capabilities or package structure, since their implementation excerpts were not provided.

Main entry points: [`packages/better-auth/src/api/dispatch.ts`](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/dispatch.ts) (API dispatch), [`packages/better-auth/src/api/routes/session.ts`](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/routes/session.ts) (Session routes), [`packages/better-auth/src/cookies/index.ts`](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/cookies/index.ts) (Cookie handling), [`packages/better-auth/src/plugins/index.ts`](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/index.ts) (Plugin ecosystem)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Auth runtime"]
  node_dispatch["API dispatch<br/>[dispatch.ts]"]
  node_auth_routes["Core auth routes<br/>[sign-in.ts]"]
  node_session["Session routes<br/>[session.ts]"]
end

subgraph group_identity["Identity flows"]
  node_oauth["OAuth flows<br/>[callback.ts]"]
  node_social["Social providers<br/>[index.ts]"]
  node_otp["Email OTP<br/>[routes.ts]"]
  node_passkey["Passkey flows<br/>[routes.ts]"]
  node_sso["SAML SSO<br/>[saml-pipeline.ts]"]
  node_device["Device authorization<br/>[routes.ts]"]
end

subgraph group_extensions["Authorization plugins"]
  node_admin["Admin operations<br/>[routes.ts]"]
  node_organization["Organizations<br/>[organization.ts]"]
  node_twofactor["Two-factor auth<br/>[index.ts]"]
  node_plugin_ecosystem["Plugin ecosystem<br/>[index.ts]"]
end

subgraph group_state["Session and storage"]
  node_cookies["Cookie handling<br/>[index.ts]"]
  node_cookie_store["Cookie session store<br/>[session-store.ts]"]
  node_schema[("Auth data schema<br/>[schema.ts]")]
  node_database_adapters["Database adapters<br/>[index.ts]"]
  node_redis[("Redis secondary storage<br/>[redis-storage.ts]")]
end

subgraph group_clients["Clients and adapters"]
  node_client_sdk["Client SDKs<br/>[vanilla.ts]"]
  node_framework_integrations["Framework integrations<br/>[next-js.ts]"]
end

node_application(("Application request"))
node_response(("Auth response"))

node_application -->|"sends request"| node_dispatch
node_dispatch -->|"dispatches endpoints"| node_auth_routes
node_dispatch -->|"dispatches endpoints"| node_session
node_auth_routes -.->|"sets session"| node_cookies
node_session -->|"reads cookies"| node_cookies
node_session -->|"uses session store"| node_cookie_store
node_cookies -->|"parses user data"| node_schema
node_auth_routes -.->|"uses auth schema"| node_schema
node_dispatch -.->|"routes OAuth"| node_oauth
node_oauth -.->|"uses providers"| node_social
node_dispatch -.->|"routes OTP"| node_otp
node_otp -->|"sets session cookie"| node_cookies
node_dispatch -.->|"routes passkeys"| node_passkey
node_passkey -->|"sets session cookie"| node_cookies
node_dispatch -.->|"routes SAML"| node_sso
node_dispatch -.->|"routes device flow"| node_device
node_device -->|"checks session"| node_session
node_plugin_ecosystem -.->|"provides extensions"| node_admin
node_plugin_ecosystem -.->|"provides extensions"| node_organization
node_plugin_ecosystem -.->|"provides extensions"| node_twofactor
node_admin -->|"checks session"| node_session
node_dispatch -.->|"supports plugins"| node_plugin_ecosystem
node_schema -.->|"maps auth data"| node_database_adapters
node_client_sdk -.->|"calls auth API"| node_dispatch
node_framework_integrations -.->|"integrates requests"| node_dispatch
node_redis -.->|"backs secondary storage"| node_cookie_store
node_dispatch -.->|"returns result"| node_response

click node_dispatch "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/dispatch.ts"
click node_auth_routes "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/routes/sign-in.ts"
click node_session "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/routes/session.ts"
click node_oauth "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/oauth2/callback.ts"
click node_social "https://github.com/better-auth/better-auth/blob/main/packages/core/src/social-providers/index.ts"
click node_otp "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/email-otp/routes.ts"
click node_passkey "https://github.com/better-auth/better-auth/blob/main/packages/passkey/src/routes.ts"
click node_sso "https://github.com/better-auth/better-auth/blob/main/packages/sso/src/routes/saml-pipeline.ts"
click node_device "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/device-authorization/routes.ts"
click node_admin "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/admin/routes.ts"
click node_organization "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/organization/organization.ts"
click node_twofactor "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/two-factor/index.ts"
click node_plugin_ecosystem "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/index.ts"
click node_cookies "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/cookies/index.ts"
click node_cookie_store "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/cookies/session-store.ts"
click node_schema "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/db/schema.ts"
click node_database_adapters "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/adapters/index.ts"
click node_client_sdk "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/client/vanilla.ts"
click node_framework_integrations "https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/integrations/next-js.ts"
click node_redis "https://github.com/better-auth/better-auth/blob/main/packages/redis-storage/src/redis-storage.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_dispatch,node_auth_routes,node_session toneBlue
class node_oauth,node_social,node_otp,node_passkey,node_sso,node_device toneAmber
class node_admin,node_organization,node_twofactor,node_plugin_ecosystem,node_application toneMint
class node_cookies,node_cookie_store,node_schema,node_database_adapters,node_redis toneRose
class node_client_sdk,node_framework_integrations,node_response toneIndigo
```

## Components

### Auth runtime

- [API dispatch](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/dispatch.ts): `packages/better-auth/src/api/dispatch.ts`
- [Core auth routes](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/routes/sign-in.ts): `packages/better-auth/src/api/routes/sign-in.ts`
- [Session routes](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/api/routes/session.ts): `packages/better-auth/src/api/routes/session.ts`

### Identity flows

- [OAuth flows](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/oauth2/callback.ts): `packages/better-auth/src/oauth2/callback.ts`
- [Social providers](https://github.com/better-auth/better-auth/blob/main/packages/core/src/social-providers/index.ts): `packages/core/src/social-providers/index.ts`
- [Email OTP](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/email-otp/routes.ts): `packages/better-auth/src/plugins/email-otp/routes.ts`
- [Passkey flows](https://github.com/better-auth/better-auth/blob/main/packages/passkey/src/routes.ts): `packages/passkey/src/routes.ts`
- [SAML SSO](https://github.com/better-auth/better-auth/blob/main/packages/sso/src/routes/saml-pipeline.ts): `packages/sso/src/routes/saml-pipeline.ts`
- [Device authorization](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/device-authorization/routes.ts): `packages/better-auth/src/plugins/device-authorization/routes.ts`

### Authorization plugins

- [Admin operations](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/admin/routes.ts): `packages/better-auth/src/plugins/admin/routes.ts`
- [Organizations](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/organization/organization.ts): `packages/better-auth/src/plugins/organization/organization.ts`
- [Two-factor auth](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/two-factor/index.ts): `packages/better-auth/src/plugins/two-factor/index.ts`
- [Plugin ecosystem](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/index.ts): `packages/better-auth/src/plugins/index.ts`

### Session and storage

- [Cookie handling](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/cookies/index.ts): `packages/better-auth/src/cookies/index.ts`
- [Cookie session store](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/cookies/session-store.ts): `packages/better-auth/src/cookies/session-store.ts`
- [Auth data schema](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/db/schema.ts): `packages/better-auth/src/db/schema.ts`
- [Database adapters](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/adapters/index.ts): `packages/better-auth/src/adapters/index.ts`
- [Redis secondary storage](https://github.com/better-auth/better-auth/blob/main/packages/redis-storage/src/redis-storage.ts): `packages/redis-storage/src/redis-storage.ts`

### Clients and adapters

- [Client SDKs](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/client/vanilla.ts): `packages/better-auth/src/client/vanilla.ts`
- [Framework integrations](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/integrations/next-js.ts): `packages/better-auth/src/integrations/next-js.ts`

### Other components

- **Application request**
- **Auth response**

## Connections

- Application request → API dispatch: sends request
- API dispatch → Core auth routes: dispatches endpoints
- API dispatch → Session routes: dispatches endpoints
- Core auth routes → Cookie handling: sets session
- Session routes → Cookie handling: reads cookies (evidence: `packages/better-auth/src/api/routes/session.ts`)
- Session routes → Cookie session store: uses session store (evidence: `packages/better-auth/src/api/routes/session.ts`)
- Cookie handling → Auth data schema: parses user data (evidence: `packages/better-auth/src/cookies/index.ts`)
- Core auth routes → Auth data schema: uses auth schema
- API dispatch → OAuth flows: routes OAuth
- OAuth flows → Social providers: uses providers
- API dispatch → Email OTP: routes OTP
- Email OTP → Cookie handling: sets session cookie (evidence: `packages/better-auth/src/plugins/email-otp/routes.ts`)
- API dispatch → Passkey flows: routes passkeys
- Passkey flows → Cookie handling: sets session cookie (evidence: `packages/passkey/src/routes.ts`)
- API dispatch → SAML SSO: routes SAML
- API dispatch → Device authorization: routes device flow
- Device authorization → Session routes: checks session (evidence: `packages/better-auth/src/plugins/device-authorization/routes.ts`)
- Plugin ecosystem → Admin operations: provides extensions
- Plugin ecosystem → Organizations: provides extensions
- Plugin ecosystem → Two-factor auth: provides extensions
- Admin operations → Session routes: checks session (evidence: `packages/better-auth/src/plugins/admin/routes.ts`)
- API dispatch → Plugin ecosystem: supports plugins
- Auth data schema → Database adapters: maps auth data
- Client SDKs → API dispatch: calls auth API
- Framework integrations → API dispatch: integrates requests
- Redis secondary storage → Cookie session store: backs secondary storage
- API dispatch → Auth response: returns result
