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

Install Better Auth

Adapter imports come from the better-auth package, so you must install it in your app.

pnpm add better-auth

better-auth is a peer dependency of @onmax/nuxt-better-auth. Keep the major versions aligned to avoid mismatches.

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/cli@latest generate

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

CLI config (Nuxt)

The CLI reads a Better Auth instance from auth.ts, not your server/auth.config.ts. Add a dedicated auth.ts file for CLI generation.

auth.ts
import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { drizzle } from 'drizzle-orm/node-postgres'

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

export const auth = betterAuth({
  database: drizzleAdapter(db, { provider: 'pg' }),
  emailAndPassword: { enabled: true },
})

If you use Prisma or Kysely, swap the adapter section to match your setup.

Use --config if you place the file elsewhere:

npx @better-auth/cli@latest generate --config server/auth/auth.ts

Keep this config in sync with server/auth.config.ts so the generated schema matches your runtime plugins and options.

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.