Drizzle-backed authentication storage
Persist Auth.js users, accounts, and related records with Drizzle ORM.
Auth.js · Function
DrizzleAdapter connects Auth.js to a supported Drizzle ORM database. Pass the database instance and optionally a custom schema; it returns an Auth.js Adapter.
DrizzleAdapter connects Auth.js to a Drizzle ORM database and returns an Auth.js Adapter implementation. It infers the supported SQL flavor from the database object and uses the default schema unless a custom schema is supplied as the second argument.
function DrizzleAdapter<SqlFlavor>(db, schema?): Adapter| Name | Type | Description |
|---|---|---|
db | SqlFlavor | A supported Drizzle PostgreSQL, MySQL, or SQLite database instance. |
schema | DefaultSchema<SqlFlavor> | Optional custom table schema; defaults to the schema for the database flavor. |
Adapter
import NextAuth from "next-auth"
import { DrizzleAdapter } from "@auth/drizzle-adapter"
import { db } from "./schema.ts"
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: DrizzleAdapter(db),
providers: [],
})Pass the configured Drizzle database to the adapter, then assign the resulting adapter in the Auth.js configuration.
Persist Auth.js users, accounts, and related records with Drizzle ORM.
Use PostgreSQL, MySQL, or SQLite through a typed Drizzle database instance.
Map Auth.js to application-specific table definitions.
DrizzleAdapter accepts a Drizzle database instance and an optional schema, then returns an Auth.js Adapter. The database type determines the SQL flavor and corresponding default schema.
Follow-up: What should be supplied for non-default tables? Pass the custom schema as the adapter's second argument so Auth.js uses those table definitions.
Install drizzle-orm and @auth/drizzle-adapter, and set up Drizzle ORM and Drizzle Kit before configuring the adapter. The adapter is then provided through the adapter property in NextAuth configuration.
Follow-up: What database setup remains necessary? Create the schema, configure a supported driver and drizzle.config.ts, generate a migration, and apply it.
The optional sessionsTable is needed for the database session strategy. The optional verificationTokensTable is needed when a Magic Link provider is used.
Follow-up: Why might a project omit one of these tables? The relevant Auth.js feature may be using another persistence path or may not be enabled.
@auth/drizzle-adapterAdapterDefaultSchemaSqlFlavorOptionsSQLiteDrizzleAdapterPostgresDrizzleAdapterMySqlDrizzleAdapter