This library is in early development. Expect breaking changes.
Guides

Custom Database

Use your own database with Drizzle, Prisma, or Kysely adapters.

Better Auth supports any database through adapters. Use this guide when you need full control over your database setup or can't use NuxtHub.

When to Use This

ScenarioRecommended
Quick start, managed infraNuxtHub
OAuth-only, no persistenceDatabase-less Mode
Existing database, custom setupThis guide

Drizzle Adapter

server/auth.config.ts
import { defineServerAuth } from '@onmax/nuxt-better-auth/config'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { drizzle } from 'drizzle-orm/node-postgres'

const db = drizzle(process.env.DATABASE_URL!)

export default defineServerAuth({
  database: drizzleAdapter(db, { provider: 'pg' }),
  emailAndPassword: { enabled: true },
})

Prisma Adapter

server/auth.config.ts
import { defineServerAuth } from '@onmax/nuxt-better-auth/config'
import { prismaAdapter } from 'better-auth/adapters/prisma'
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

export default defineServerAuth({
  database: prismaAdapter(prisma, { provider: 'postgresql' }),
  emailAndPassword: { enabled: true },
})

Kysely Adapter

server/auth.config.ts
import { defineServerAuth } from '@onmax/nuxt-better-auth/config'
import { kyselyAdapter } from 'better-auth/adapters/kysely'
import { Kysely, PostgresDialect } from 'kysely'
import { Pool } from 'pg'

const db = new Kysely({
  dialect: new PostgresDialect({ pool: new Pool({ connectionString: process.env.DATABASE_URL }) }),
})

export default defineServerAuth({
  database: kyselyAdapter(db, { dialect: 'postgres', type: 'pg' }),
  emailAndPassword: { enabled: true },
})

Schema Setup

You must create the required tables manually. Better Auth provides a CLI to generate schemas:

npx better-auth generate

This generates migration files for your adapter. Apply them with your database tool.

See Better Auth database docs for full schema reference and adapter options.

Environment Variables

Store your connection string securely:

.env
DATABASE_URL="postgresql://user:password@localhost:5432/myapp"
Never commit database credentials to version control. Use environment variables in production.