NuxtHub provides the easiest way to add database persistence to your authentication. Enable NuxtHub and this module generates and manages your database schema.
better-auth beta. Install with pnpm add better-auth@beta.twoFactor, passkeynpx nuxt db migrate for productionpnpm add @nuxthub/core@^0.10.5
export default defineNuxtConfig({
modules: [
'@nuxthub/core',
'@onmax/nuxt-better-auth'
],
hub: {
db: 'sqlite' // 'sqlite' | 'postgresql' | 'mysql'
}
})
The schema is generated at build time. Restart after configuration changes.
The hub.db option accepts a string shorthand or full object:
export default defineNuxtConfig({
hub: {
db: 'sqlite' // shorthand: 'sqlite' | 'postgresql' | 'mysql'
// or full object:
// db: { dialect: 'postgresql', /* additional options */ }
}
})
Enable KV storage for faster session lookups:
export default defineNuxtConfig({
hub: {
db: 'sqlite',
kv: true
},
auth: {
secondaryStorage: true
}
})
Sessions are cached in NuxtHub KV, reducing database queries.
NuxtHub handles migrations automatically in most deployments. For manual control:
If you need to run migrations manually:
# Generate migrations from schema changes
npx nuxt db generate
# Apply pending migrations
npx nuxt db migrate
Add to your GitHub Actions workflow:
- name: Run migrations
run: npx nuxt db migrate
env:
NUXT_HUB_PROJECT_KEY: ${{ secrets.NUXTHUB_PROJECT_KEY }}
The module analyzes your server/auth.config.ts at build time:
hub:db:schema:extend hookWith NuxtHub, you get access to the Drizzle database instance in your server config:
import { defineServerAuth } from '@onmax/nuxt-better-auth/config'
export default defineServerAuth(({ db }) => ({
// db is the Drizzle instance from NuxtHub
}))
Create application tables that reference auth tables by importing schema from hub:db. NuxtHub automatically merges your custom schemas with Better Auth tables.
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core'
import { schema } from '@nuxthub/db'
export const posts = sqliteTable('posts', {
id: text('id').primaryKey(),
title: text('title').notNull(),
authorId: text('author_id').notNull()
.references(() => schema.user.id),
createdAt: integer('created_at', { mode: 'timestamp' })
.$defaultFn(() => new Date()),
})
Reference these tables via the schema object:
schema.user - User accountsschema.session - Active sessionsschema.account - OAuth provider accountsschema.verification - Email verification tokensschema.passkey, schema.twoFactor, etc. (based on enabled plugins)Match your auth table ID types in foreign keys:
text() or varchar()uuid() when advanced.database.generateId = 'uuid'Generate and apply migrations after schema changes:
npx nuxt db generate # Generate migrations
npx nuxt db migrate # Apply (automatic in dev)
server/db/ for NuxtHub to discover them.To add fields to existing auth tables (e.g., role on user), use Better Auth's additionalFields instead of custom schemas.