|
1 | 1 | --- |
2 | 2 | name: nuxt-better-auth |
3 | | -description: Use when implementing auth in Nuxt apps with @nuxtjs/better-auth - provides client composables, server helpers, route protection, session refresh helpers, and Better Auth plugin integration. |
| 3 | +description: Guides authentication in Nuxt apps using @nuxtjs/better-auth. Use when installing or configuring the module, using its client or server APIs, protecting routes, refreshing sessions, or integrating Better Auth plugins. |
4 | 4 | license: MIT |
5 | 5 | --- |
6 | 6 |
|
7 | 7 | # Nuxt Better Auth |
8 | 8 |
|
9 | | -Authentication module for Nuxt built on [Better Auth](https://www.better-auth.com/). It adds Nuxt-specific setup, route protection, server helpers, and typed auth state. |
| 9 | +Use the smallest reference that matches the task. |
10 | 10 |
|
11 | | -> Alpha status: the package is still pre-stable. Verify behavior against the current docs and source before relying on edge cases. |
| 11 | +## Pick a reference |
12 | 12 |
|
13 | | -## When to Use |
| 13 | +| Task | Read | |
| 14 | +| --- | --- | |
| 15 | +| Install the module, configure environment variables, or create config files | [references/installation.md](references/installation.md) | |
| 16 | +| Read client session state, build auth forms, call plugins, or fetch auth-bound data | [references/client-auth.md](references/client-auth.md) | |
| 17 | +| Read or enforce sessions in server handlers, create sessions, or refresh cached session data | [references/server-auth.md](references/server-auth.md) | |
| 18 | +| Protect pages or API routes and configure redirects | [references/route-protection.md](references/route-protection.md) | |
| 19 | +| Register Better Auth plugins and their client companions | [references/plugins.md](references/plugins.md) | |
| 20 | +| Configure NuxtHub, generated schema, or secondary storage | [references/database.md](references/database.md) | |
| 21 | +| Connect a Nuxt frontend to an external Better Auth server | [references/client-only.md](references/client-only.md) | |
| 22 | +| Use inferred auth types or add project fields | [references/types.md](references/types.md) | |
14 | 23 |
|
15 | | -- Installing/configuring `@nuxtjs/better-auth` |
16 | | -- Implementing sign-in, sign-up, sign-out, or custom auth flows |
17 | | -- Protecting routes (client and server) |
18 | | -- Accessing user session in API routes |
19 | | -- Refreshing session state after custom auth endpoints or server-side session changes |
20 | | -- Integrating Better Auth plugins (admin, passkey, 2FA) |
21 | | -- Setting up database with NuxtHub |
22 | | -- Using clientOnly mode for external auth backends |
| 24 | +## Rules that apply across tasks |
23 | 25 |
|
24 | | -## Available Guidance |
| 26 | +- Confirm the consumer's installed package version supports each API you use. The published skill may be newer than the consumer's lockfile. |
| 27 | +- Enforce protected API reads and mutations server-side with `requireUserSession(event)`. Route rules and page meta also control navigation. |
| 28 | +- Navigate only to validated local redirect paths. |
| 29 | +- In `clientOnly` mode, do not use local server helpers or expect SSR session hydration. |
| 30 | +- After server-side changes to session payload fields, call `refreshSessionCookieCache(event)`. Wrap custom client auth endpoints that create or change the current session with `runWithSessionRefresh()`. |
25 | 31 |
|
26 | | -| File | Topics | |
27 | | -| -------------------------------------------------------------------- | ---------------------------------------------------------------------- | |
28 | | -| **[references/installation.md](references/installation.md)** | install flow, env vars, config files | |
29 | | -| **[references/client-auth.md](references/client-auth.md)** | `useUserSession`, client methods, redirects, loading states, custom actions | |
30 | | -| **[references/server-auth.md](references/server-auth.md)** | `serverAuth`, session helpers, `refreshSessionCookieCache`, API enforcement | |
31 | | -| **[references/route-protection.md](references/route-protection.md)** | route rules, page meta, API protection | |
32 | | -| **[references/plugins.md](references/plugins.md)** | plugin pairing between server and client | |
33 | | -| **[references/database.md](references/database.md)** | NuxtHub schema generation, secondary storage | |
34 | | -| **[references/client-only.md](references/client-only.md)** | external Better Auth backends and `clientOnly` mode | |
35 | | -| **[references/types.md](references/types.md)** | public auth types and augmentation | |
36 | | - |
37 | | -## Usage Pattern |
38 | | - |
39 | | -- Installing module? → [references/installation.md](references/installation.md) |
40 | | -- Login/signup forms? → [references/client-auth.md](references/client-auth.md) |
41 | | -- API route protection? → [references/server-auth.md](references/server-auth.md) |
42 | | -- Route rules/page meta? → [references/route-protection.md](references/route-protection.md) |
43 | | -- Using plugins? → [references/plugins.md](references/plugins.md) |
44 | | -- Database setup? → [references/database.md](references/database.md) |
45 | | -- External auth backend? → [references/client-only.md](references/client-only.md) |
46 | | -- TypeScript types? → [references/types.md](references/types.md) |
47 | | - |
48 | | -Do not load every reference file by default. Pick the smallest file that matches the task. |
49 | | - |
50 | | -## Key Concepts |
51 | | - |
52 | | -| Concept | Description | |
53 | | -| ---------------------- | --------------------------------------------------------------- | |
54 | | -| `useUserSession()` | Client composable - user, session, loggedIn, refresh, sign-out | |
55 | | -| `runWithSessionRefresh()` | Refresh local session after custom auth endpoints | |
56 | | -| `requireUserSession()` | Server helper - throws 401/403 if not authenticated | |
57 | | -| `refreshSessionCookieCache()` | Refresh Better Auth's cached session cookie after server updates | |
58 | | -| `auth` route mode | `'user'`, `'guest'`, `{ user: {...} }`, or `false` | |
59 | | -| `serverAuth()` | Get Better Auth instance in server routes | |
60 | | - |
61 | | -## Quick Reference |
62 | | - |
63 | | -```ts |
64 | | -// Client: useUserSession() |
65 | | -const { user, loggedIn, signIn, signOut } = useUserSession() |
66 | | -await signIn.email({ email, password }, { onSuccess: () => navigateTo('/') }) |
67 | | -``` |
68 | | - |
69 | | -```ts |
70 | | -// Client: custom auth endpoint |
71 | | -await runWithSessionRefresh(() => $fetch('/api/custom-login', { method: 'POST', body })) |
72 | | -``` |
73 | | - |
74 | | -```ts |
75 | | -// Server: requireUserSession() |
76 | | -const { user } = await requireUserSession(event, { user: { role: 'admin' } }) |
77 | | -``` |
78 | | - |
79 | | -```ts |
80 | | -// Server: after updating fields returned by session helpers |
81 | | -await updateCurrentUser(event) |
82 | | -await refreshSessionCookieCache(event) |
83 | | -``` |
84 | | - |
85 | | -```ts |
86 | | -// nuxt.config.ts: Route protection |
87 | | -routeRules: { |
88 | | - '/admin/**': { auth: { user: { role: 'admin' } } }, |
89 | | - '/login': { auth: 'guest' }, |
90 | | - '/app/**': { auth: 'user' } |
91 | | -} |
92 | | -``` |
93 | | - |
94 | | -Broad rules such as `'/**': { auth: 'user' }` skip framework/module internals like `/_nuxt/**`, `/_ipx/**`, `/api/auth/**`, `/api/_better-auth/**`, and `/api/_nuxt_icon/**`. |
| 32 | +Before finishing an implementation, run the consumer's typecheck and the narrowest relevant auth test. |
95 | 33 |
|
96 | 34 | ## Resources |
97 | 35 |
|
98 | 36 | - [Documentation site](https://better-auth.nuxt.dev) |
99 | | -- [Better Auth Docs](https://www.better-auth.com/) |
| 37 | +- [Better Auth docs](https://www.better-auth.com/) |
0 commit comments