Adapters
AuthHero uses a pluggable adapter pattern to decouple authentication logic from data storage. The core authhero package never touches a database directly — it works through adapter interfaces.
The Adapter Pattern
The DataAdapters interface defines CRUD operations for every entity in the system:
interface DataAdapters {
users: UsersAdapter;
clients: ClientsAdapter;
connections: ConnectionsAdapter;
sessions: SessionsAdapter;
codes: CodesAdapter;
tickets: TicketsAdapter;
keys: KeysAdapter;
// ... ~39 entity modules in total
}You inject a concrete adapter when initializing AuthHero:
import { init } from "authhero";
import createAdapters from "@authhero/drizzle";
const dataAdapter = createAdapters(db);
const { app } = init({ dataAdapter });Reading the adapters during a request
Each route group wraps the adapter you pass in with request-scoped layers (caching, deduplication, hooks), so every request gets its own adapter stack. Middleware and hooks running inside a request read that stack from ctx.var.data.
ctx.env.data points at the same object for now but is deprecated, and will be removed in a future major release. Don't write to ctx.env during a request: on Cloudflare Workers every request in an isolate shares the same env object.
Built-in Adapters
| Adapter | Database | Best For |
|---|---|---|
@authhero/drizzle | SQLite, Cloudflare D1 | New deployments (primary adapter) |
@authhero/kysely-adapter | SQLite, MySQL (PlanetScale) | Existing deployments |
@authhero/aws-adapter | DynamoDB | AWS Lambda deployments |
@authhero/cloudflare-adapter | Platform services (custom domains, cache, R2 storage, logs, rate limiting) — used alongside a database adapter | Cloudflare Workers deployments |
Layering Adapters
One of the most powerful features is the ability to layer adapters. The createPassthroughAdapter() utility lets you compose adapters that sync writes to multiple backends simultaneously.
This enables:
Database Migration
Write to both the old and new database during migration, then switch reads:
const adapters = createPassthroughAdapter(
primaryAdapter, // reads come from here
secondaryAdapter // writes are synced here too
);Fallback
Read from the primary adapter, fall back to a secondary if data isn't found:
const adapters = createFallbackAdapter(
primaryAdapter,
legacyAdapter
);Caching
Layer a cache adapter in front of a database adapter for frequently accessed data like tenant settings and signing keys.
Writing Custom Adapters
Implement the DataAdapters interface (from @authhero/adapter-interfaces) to support any database or storage backend. Each adapter module handles one entity type with standard CRUD operations.
See Customization — Adapter Interfaces for implementation details.