Skip to content

Commit f18f718

Browse files
authored
docs: add copy-paste AI prompts (#273)
1 parent 776331f commit f18f718

File tree

12 files changed

+182
-6
lines changed

12 files changed

+182
-6
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script setup lang="ts">
2+
const open = ref(false)
3+
</script>
4+
5+
<template>
6+
<UCollapsible v-model:open="open" class="my-4">
7+
<UButton variant="ghost" size="sm" :icon="open ? 'i-lucide-chevron-down' : 'i-lucide-chevron-right'" trailing-icon="i-lucide-sparkles" label="AI Prompt" @click="open = !open" />
8+
<template #content>
9+
<div class="pt-2">
10+
<ContentSlot :use="$slots.default" unwrap="p" />
11+
</div>
12+
</template>
13+
</UCollapsible>
14+
</template>

docs/content/1.getting-started/1.installation.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ description: Learn how to add Nuxt Better Auth to your Nuxt project.
44
navigation.icon: i-lucide-download
55
---
66

7+
::code-collapse
8+
9+
```txt [Prompt]
10+
Install @onmax/nuxt-better-auth in my Nuxt 4 app.
11+
12+
- Run `npx nuxi module add @onmax/nuxt-better-auth@alpha`
13+
- Set `BETTER_AUTH_SECRET` in `.env` (at least 32 chars, high entropy). Optionally prefix with `NUXT_` for runtime config
14+
- Optionally set `NUXT_PUBLIC_SITE_URL` for non-auto-detected platforms
15+
- Create `server/auth.config.ts` using `defineServerAuth` from `@onmax/nuxt-better-auth/config`
16+
- Create `app/auth.config.ts` using `defineClientAuth` from `@onmax/nuxt-better-auth/config`
17+
- The module auto-injects `secret` and `baseURL` — do not configure them manually
18+
19+
Read more: https://better-auth.nuxt.dev/raw/getting-started/installation.md
20+
Source: https://github.com/nuxt-modules/better-auth
21+
```
22+
23+
::
24+
725
## Prerequisites
826

927
- Nuxt v4.0+

docs/content/1.getting-started/2.configuration.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ description: Configure the module options and your Better Auth server instance.
44
navigation.icon: i-lucide-settings
55
---
66

7+
::code-collapse
8+
9+
```txt [Prompt]
10+
Configure @onmax/nuxt-better-auth module options and server auth.
11+
12+
- In `nuxt.config.ts`, set `auth.redirects` (login, guest, authenticated, logout) and `auth.preserveRedirect`
13+
- Use `routeRules` or `nitro.routeRules` to define per-route auth: `{ auth: { only: 'user', redirectTo: '/login' } }`
14+
- In `server/auth.config.ts`, use `defineServerAuth` (object or function syntax) to configure plugins and providers
15+
- The function syntax receives `ctx` with `runtimeConfig` and `db` (NuxtHub)
16+
- The module auto-injects `secret` and `baseURL` — do not set them in defineServerAuth
17+
- Base URL priority: runtimeConfig > request URL > platform env vars > localhost
18+
- Set `NUXT_PUBLIC_SITE_URL` for custom domains or deterministic OAuth callbacks
19+
20+
Read more: https://better-auth.nuxt.dev/raw/getting-started/configuration.md
21+
Source: https://github.com/nuxt-modules/better-auth
22+
```
23+
24+
::
25+
726
## Module Configuration
827

928
```ts [nuxt.config.ts]

docs/content/1.getting-started/3.client-setup.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ description: Configure the client-side authentication client.
44
navigation.icon: i-lucide-monitor
55
---
66

7+
::code-collapse
8+
9+
```txt [Prompt]
10+
Set up the client auth config for @onmax/nuxt-better-auth.
11+
12+
- Create `app/auth.config.ts` with a default export using `defineClientAuth` from `@onmax/nuxt-better-auth/config`
13+
- Object syntax: `defineClientAuth({})` or function syntax: `defineClientAuth(({ siteUrl }) => ({}))`
14+
- Add client plugin equivalents for every server plugin (e.g. `adminClient()` from `better-auth/client/plugins`)
15+
- The module calls the factory with the correct `baseURL` at runtime
16+
- `useUserSession()` is auto-imported and provides `user`, `session`, `loggedIn`, `ready`, `signIn`, `signUp`, `signOut`
17+
18+
Read more: https://better-auth.nuxt.dev/raw/getting-started/client-setup.md
19+
Source: https://github.com/nuxt-modules/better-auth
20+
```
21+
22+
::
23+
724
## Create the Client Config
825

926
Create `app/auth.config.ts` with a default export using `defineClientAuth`.

docs/content/1.getting-started/4.type-augmentation.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ description: Learn how the module automatically infers types from your configura
44
navigation.icon: i-lucide-type
55
---
66

7+
::code-collapse
8+
9+
```txt [Prompt]
10+
Set up type augmentation for @onmax/nuxt-better-auth plugins.
11+
12+
- Types are automatically inferred from plugins in `server/auth.config.ts` — no extra config needed
13+
- For manual augmentation, create `nuxt-better-auth.d.ts` next to `nuxt.config.ts`
14+
- Import `#nuxt-better-auth` and declare module to extend `AuthUser` or `AuthSession` interfaces
15+
- If types don't update: restart TS server, run `npx nuxi prepare`, check config for syntax errors
16+
- Ensure `.d.ts` files are included by TypeScript (`tsconfig.json` include or `/// <reference>`)
17+
18+
Read more: https://better-auth.nuxt.dev/raw/getting-started/type-augmentation.md
19+
Source: https://github.com/nuxt-modules/better-auth
20+
```
21+
22+
::
23+
724
When you add Better Auth plugins that extend the user or session objects, TypeScript needs to know about these new fields. The module automatically infers types from your configuration, but you can also augment types manually.
825

926
## Automatic Type Inference

docs/content/1.getting-started/5.schema-generation.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ description: Auto-generate Drizzle ORM schemas for Better Auth tables when using
55
navigation.icon: i-lucide-database
66
---
77

8+
::code-collapse
9+
10+
```txt [Prompt]
11+
Set up auto schema generation for @onmax/nuxt-better-auth with NuxtHub.
12+
13+
- Requires NuxtHub integration — the module generates Drizzle ORM schemas at build time
14+
- Supports sqlite (default), postgresql, and mysql dialects via `hub.db.dialect` in `nuxt.config.ts`
15+
- The module reads `server/auth.config.ts` plugins to determine required tables (user, session, account, verification + plugin tables)
16+
- After adding/removing plugins: restart the dev server to regenerate schemas
17+
- For production (Cloudflare D1): run `npx nuxt db migrate`
18+
- No manual Drizzle schema writing needed for auth tables
19+
20+
Read more: https://better-auth.nuxt.dev/raw/getting-started/schema-generation.md
21+
Source: https://github.com/nuxt-modules/better-auth
22+
```
23+
24+
::
25+
826
::note
927
This feature requires **NuxtHub**. See [NuxtHub Integration](/integrations/nuxthub) for setup.
1028
::

docs/content/2.core-concepts/1.server-auth.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ title: serverAuth()
33
description: When to reach for the full Better Auth server instance.
44
---
55

6+
::code-collapse
7+
8+
```txt [Prompt]
9+
Use server-side auth utilities in @onmax/nuxt-better-auth.
10+
11+
- `serverAuth(event?)` — returns the Better Auth instance. Pass `event` in Nitro handlers for request-aware base URLs
12+
- `getUserSession(event)` — get current session (auto-imported in `server/`)
13+
- `requireUserSession(event, options?)` — throws 401 if not authenticated, supports role matching
14+
- `getRequestSession(event)` — request-cached session, preferred over repeated `getUserSession` in same request
15+
- All server utils are auto-imported, no import statements needed
16+
17+
Read more: https://better-auth.nuxt.dev/raw/core-concepts/server-auth.md
18+
Source: https://github.com/nuxt-modules/better-auth
19+
```
20+
21+
::
22+
623
`serverAuth(event?)` returns the Better Auth instance (module-level singleton). In Nitro handlers, you should pass `event` so the module can resolve request-aware base URLs on initialization. Outside request contexts (seed scripts, tasks, plugins), you can call `serverAuth()` without an event.
724

825
## When to Use What

docs/content/2.core-concepts/2.sessions.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@ title: Sessions
33
description: Access reactive session state with SSR support using `useUserSession()`.
44
---
55

6+
::code-collapse
7+
8+
```txt [Prompt]
9+
Handle sessions with @onmax/nuxt-better-auth using useUserSession().
10+
11+
- `useUserSession()` is auto-imported and returns: user, session, loggedIn, ready, signIn, signUp, signOut, fetchSession
12+
- SSR: session is fetched server-side via cookies and hydrated — `ready` is `true` after hydration
13+
- Client: `.client` is `null` during SSR, available after hydration
14+
- Split model: use `useUserSession()` in pages/components, use `requireUserSession(event)` in server handlers
15+
- Configure session lifetime in `server/auth.config.ts` via `session.expiresIn` and `session.cookieCache`
16+
- Use `<BetterAuthState>` or `ready` ref to avoid loading flashes
17+
- Force refresh: `fetchSession({ force: true })`
18+
19+
Read more: https://better-auth.nuxt.dev/raw/core-concepts/sessions.md
20+
Source: https://github.com/nuxt-modules/better-auth
21+
```
22+
23+
::
24+
625
```ts [pages/dashboard.vue]
726
const {
827
user,

docs/content/2.core-concepts/3.route-protection.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@ title: Route Protection
33
description: Learn how to protect your pages and API routes using a layered approach.
44
---
55

6+
::code-collapse
7+
8+
```txt [Prompt]
9+
Protect routes and API endpoints with @onmax/nuxt-better-auth.
10+
11+
- Route Rules in `nuxt.config.ts`: `routeRules: { '/app/**': { auth: { only: 'user', redirectTo: '/login' } } }`
12+
- Guest-only pages: `{ auth: { only: 'guest', redirectTo: '/app' } }`
13+
- Role-based: `{ auth: { user: { role: 'admin' } } }` — arrays use OR logic
14+
- Page Meta: `definePageMeta({ auth: 'user' })` for per-page control
15+
- Server API: `await requireUserSession(event)` — throws 401 if not authenticated
16+
- Role + custom rules: `requireUserSession(event, { user: { role: 'admin' }, rule: ({ user }) => user.verified })`
17+
- Route rules auto-sync to client router for instant redirects
18+
- Always protect API endpoints with `requireUserSession` — route rules are UX only
19+
20+
Read more: https://better-auth.nuxt.dev/raw/core-concepts/route-protection.md
21+
Source: https://github.com/nuxt-modules/better-auth
22+
```
23+
24+
::
25+
626
## Quick Reference
727

828
| Method | Scope | Use Case |

docs/content/2.core-concepts/4.auto-imports-aliases.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ title: Auto‑Imports and Aliases
33
description: What the module registers for you.
44
---
55

6+
::code-collapse
7+
8+
```txt [Prompt]
9+
Use auto-imported utilities from @onmax/nuxt-better-auth.
10+
11+
- Client (auto-imported in Vue): `useUserSession()`, `useSignIn()`, `useSignUp()`
12+
- Server (auto-imported in `server/`): `serverAuth(event?)`, `getRequestSession(event)`, `getUserSession(event)`, `requireUserSession(event, options?)`
13+
- Component: `<BetterAuthState>` for auth-ready rendering
14+
- Alias `#nuxt-better-auth` exports types: `AuthUser`, `AuthSession`, `AuthSocialProviderId`
15+
- Use `getRequestSession(event)` over repeated `getUserSession(event)` calls — it caches per request
16+
17+
Read more: https://better-auth.nuxt.dev/raw/core-concepts/auto-imports-aliases.md
18+
Source: https://github.com/nuxt-modules/better-auth
19+
```
20+
21+
::
22+
623
## Auto‑imports
724

825
**Client**

0 commit comments

Comments
 (0)