Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .nuxtrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
setups.nuxt-convex="0.0.1"
setups.nuxt-convex="0.0.6"
setups.@nuxt/test-utils="3.23.0"
75 changes: 48 additions & 27 deletions docs/content/5.integrations/1.better-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,50 @@ title: Better Auth
description: Use Convex as the database for Better Auth authentication.
---

# Better Auth Integration
Use Convex as the database backend for [Better Auth](https://better-auth.com) with `@onmax/nuxt-better-auth`.

Use Convex as the database backend for [Better Auth](https://better-auth.com) with the [`@onmax/nuxt-better-auth`](https://github.com/onmax/nuxt-better-auth) module.
`nuxt-convex` registers the `convex` database provider via the `better-auth:database:providers` hook. `@onmax/nuxt-better-auth` then generates `#auth/database` from that provider.

## How It Works
::important
This integration runs on the server. `auth.database.provider = 'convex'` is not available in `clientOnly` mode.
::

- Auth runs on **Nuxt server** (SSR-compatible, route protection)
- Convex provides the **database** via HTTP adapter
- Uses `@convex-dev/better-auth` CRUD functions
::steps

## Setup

### 1. Install Dependencies
### Install dependencies

```bash
pnpm add @onmax/nuxt-better-auth @convex-dev/better-auth better-auth
```

### 2. Configure Nuxt
### Configure Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
modules: ['nuxt-convex', '@onmax/nuxt-better-auth'],
auth: {
database: {
provider: 'convex',
// Optional override (highest priority).
// convexUrl: 'https://your-project.convex.cloud',
},
},
})
```

### 3. Set Environment Variables
### Set environment variables

```bash [.env]
CONVEX_URL=https://your-project.convex.cloud
BETTER_AUTH_SECRET=your-secret-at-least-32-characters
```

### 4. Create Server Auth Config
### Create Better Auth config files

`@onmax/nuxt-better-auth` loads these files by default:

- `server/auth.config.ts`
- `app/auth.config.ts`

```ts [server/auth.config.ts]
import { defineServerAuth } from '@onmax/nuxt-better-auth/config'
Expand All @@ -51,29 +61,36 @@ export default defineServerAuth(({ runtimeConfig }) => ({
}))
```

### 5. Create Client Auth Config

```ts [app/auth.config.ts]
import { defineClientAuth } from '@onmax/nuxt-better-auth/config'

export default defineClientAuth({})
```

### 6. Create Convex Auth Functions
### Create Convex auth functions

Export CRUD functions that the Better Auth adapter will call:
Create a `convex/auth.ts` file that exports the CRUD functions the adapter calls.

```ts [convex/auth.ts]
import { createApi } from '@convex-dev/better-auth'
import schema from './schema'

export const { create, findOne, findMany, updateOne, updateMany, deleteOne, deleteMany }
= createApi(schema, () => ({}))
export const {
create,
findOne,
findMany,
updateOne,
updateMany,
deleteOne,
deleteMany,
} = createApi(schema, () => ({}))
```

### 7. Add Auth Tables to Schema
### Add auth tables to your Convex schema

Add the Better Auth tables to `convex/schema.ts`.

Add Better Auth tables to your Convex schema:
::collapsible{title="Example schema (Better Auth tables)"}

```ts [convex/schema.ts]
import { defineSchema, defineTable } from 'convex/server'
Expand Down Expand Up @@ -138,15 +155,19 @@ export default defineSchema({
})
```

### 8. Deploy Convex
::

### Deploy Convex

```bash
npx convex deploy
```

::

## Usage

### Client-Side
### Client-side

```vue
<script setup lang="ts">
Expand All @@ -168,7 +189,7 @@ const { user, signIn, signOut } = useUserSession()
</template>
```

### Server-Side
### Server-side

```ts [server/api/protected.ts]
export default defineEventHandler(async (event) => {
Expand All @@ -179,11 +200,11 @@ export default defineEventHandler(async (event) => {

## Performance

HTTP latency to Convex (~50-200ms per DB call) is acceptable for auth operations:
HTTP latency to Convex is typically acceptable for auth operations:

- Login/signup are infrequent
- Use JWE session cookies to minimize DB reads
- Enable `cookieCache` for session caching
- Login/signup are infrequent.
- JWE session cookies minimize DB reads.
- `cookieCache` reduces repeated session lookups.

```ts [server/auth.config.ts]
export default defineServerAuth({
Expand Down
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
".": {
"types": "./dist/types.d.mts",
"import": "./dist/module.mjs"
},
"./better-auth": {
"types": "./dist/runtime/better-auth/adapter.d.ts",
"import": "./dist/runtime/better-auth/adapter.js"
}
},
"main": "./dist/module.mjs",
Expand All @@ -47,6 +51,7 @@
"dev:build": "nuxi build playground",
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare",
"playground:prepare": "nuxi prepare playground",
"pretest": "nuxt-module-build build",
"test": "vitest run",
"test:watch": "vitest",
"typecheck": "nuxt-module-build build",
Expand All @@ -55,8 +60,14 @@
"release": "bumpp && git push --follow-tags"
},
"peerDependencies": {
"better-auth": ">=1.4.9 <1.6.0",
"convex": ">=1.0.0"
},
"peerDependenciesMeta": {
"better-auth": {
"optional": true
}
},
"dependencies": {
"@nuxt/kit": "catalog:prod",
"consola": "catalog:prod",
Expand All @@ -72,6 +83,7 @@
"@nuxt/test-utils": "catalog:test",
"@types/node": "catalog:types",
"@vue/test-utils": "catalog:test",
"better-auth": "catalog:playground",
"bumpp": "catalog:",
"convex": "catalog:convex",
"eslint": "catalog:lint",
Expand Down
Loading
Loading