|
1 | 1 | --- |
2 | 2 | title: Route Protection |
3 | | -description: Protect pages with route rules or meta. |
| 3 | +description: Layered approach to protect routes on client and server. |
4 | 4 | --- |
5 | 5 |
|
6 | | -There are two protection layers: |
| 6 | +## Layer 1: Route Rules |
7 | 7 |
|
8 | | -## Client pages |
| 8 | +Declarative protection in `nuxt.config.ts`. Covers 90% of use cases: |
9 | 9 |
|
10 | | -Global route middleware reads `meta.auth` / `meta.role`: |
| 10 | +```ts |
| 11 | +export default defineNuxtConfig({ |
| 12 | + routeRules: { |
| 13 | + '/app/**': { auth: 'user' }, |
| 14 | + '/login': { auth: 'guest' }, |
| 15 | + '/admin/**': { auth: { user: { role: 'admin' } } }, |
| 16 | + '/staff/**': { auth: { user: { role: ['admin', 'moderator'] } } }, |
| 17 | + }, |
| 18 | +}) |
| 19 | +``` |
| 20 | + |
| 21 | +## Layer 2: Page Meta |
| 22 | + |
| 23 | +Per-page overrides using `definePageMeta`. Same syntax as `routeRules`: |
11 | 24 |
|
12 | 25 | ```ts |
13 | 26 | definePageMeta({ |
14 | | - auth: 'user', // or 'guest' |
15 | | - // role: 'admin', // optional |
| 27 | + auth: 'user', |
| 28 | +}) |
| 29 | + |
| 30 | +// Or with user matching |
| 31 | +definePageMeta({ |
| 32 | + auth: { user: { role: 'admin' } }, |
| 33 | +}) |
| 34 | + |
| 35 | +// Custom redirect |
| 36 | +definePageMeta({ |
| 37 | + auth: { only: 'user', redirectTo: '/subscribe' }, |
16 | 38 | }) |
17 | 39 | ``` |
18 | 40 |
|
19 | | -If a user is not logged in and `auth` resolves to `'user'`, they are redirected to `/login`. |
| 41 | +## Layer 3: Custom Middleware |
20 | 42 |
|
21 | | -## Route rules sync |
| 43 | +For complex client-side logic, create standard Nuxt route middleware: |
22 | 44 |
|
23 | | -The module copies `routeRules.auth` and `routeRules.role` into page meta at build time: |
| 45 | +```ts |
| 46 | +// middleware/premium.ts |
| 47 | +export default defineNuxtRouteMiddleware(() => { |
| 48 | + const { user } = useUserSession() |
| 49 | + |
| 50 | + if (!user.value?.subscription?.active) { |
| 51 | + return navigateTo('/pricing') |
| 52 | + } |
| 53 | +}) |
| 54 | +``` |
24 | 55 |
|
25 | 56 | ```ts |
| 57 | +// pages/premium.vue |
| 58 | +definePageMeta({ |
| 59 | + middleware: 'premium', |
| 60 | +}) |
| 61 | +``` |
| 62 | + |
| 63 | +## Layer 4: Server API Protection |
| 64 | + |
| 65 | +Use `requireUserSession()` for server-side checks: |
| 66 | + |
| 67 | +```ts |
| 68 | +// server/api/admin/stats.get.ts |
| 69 | +export default defineEventHandler(async (event) => { |
| 70 | + const { user } = await requireUserSession(event) |
| 71 | + |
| 72 | + // User is guaranteed to exist |
| 73 | + return { stats: await getStats(user.id) } |
| 74 | +}) |
| 75 | +``` |
| 76 | + |
| 77 | +With user matching: |
| 78 | + |
| 79 | +```ts |
| 80 | +// server/api/admin/users.get.ts |
| 81 | +export default defineEventHandler(async (event) => { |
| 82 | + await requireUserSession(event, { user: { role: 'admin' } }) |
| 83 | + |
| 84 | + return { users: await getAllUsers() } |
| 85 | +}) |
| 86 | +``` |
| 87 | + |
| 88 | +## Module Configuration |
| 89 | + |
| 90 | +```ts |
| 91 | +// nuxt.config.ts |
26 | 92 | export default defineNuxtConfig({ |
27 | | - routeRules: { |
28 | | - '/app/**': { auth: 'user' }, |
29 | | - '/admin/**': { auth: { role: 'admin' } }, |
30 | | - '/login': { auth: 'guest' }, |
| 93 | + auth: { |
| 94 | + redirects: { |
| 95 | + login: '/login', // Where to send unauthenticated users |
| 96 | + guest: '/', // Where to send authenticated users from guest-only pages |
| 97 | + }, |
31 | 98 | }, |
32 | 99 | }) |
33 | 100 | ``` |
34 | 101 |
|
35 | | -### `auth` values |
| 102 | +## AuthMeta Types |
| 103 | + |
| 104 | +| Value | Description | |
| 105 | +|-------|-------------| |
| 106 | +| `false` | Public, no checks | |
| 107 | +| `'guest'` | Unauthenticated only | |
| 108 | +| `'user'` | Authenticated only | |
| 109 | +| `{ only?, redirectTo?, user? }` | Object form with options | |
| 110 | + |
| 111 | +Object form options: |
| 112 | +- `only: 'guest' | 'user'` - access restriction |
| 113 | +- `redirectTo: string` - custom redirect path |
| 114 | +- `user: UserMatch` - match against user properties |
| 115 | + |
| 116 | +## UserMatch Syntax |
36 | 117 |
|
37 | | -- `false` or `undefined`: public |
38 | | -- `'guest'`: only unauthenticated users |
39 | | -- `'user'`: any authenticated user |
40 | | -- `{ only?: 'guest' | 'user', role?: string | string[], redirectTo?: string }` |
| 118 | +Match user properties for fine-grained access control: |
| 119 | + |
| 120 | +```ts |
| 121 | +// Exact match - user.role must equal 'admin' |
| 122 | +{ user: { role: 'admin' } } |
| 123 | + |
| 124 | +// OR logic - user.role must be 'admin' OR 'moderator' |
| 125 | +{ user: { role: ['admin', 'moderator'] } } |
| 126 | + |
| 127 | +// AND logic - multiple fields must all match |
| 128 | +{ user: { role: 'admin', verified: true } } |
| 129 | + |
| 130 | +// Combined - role is admin OR moderator, AND verified is true |
| 131 | +{ user: { role: ['admin', 'moderator'], verified: true } } |
| 132 | +``` |
41 | 133 |
|
42 | | -## See also |
| 134 | +## See Also |
43 | 135 |
|
44 | | -- How the module works: `/core-concepts/how-it-works` |
45 | | -- Security & caveats: `/core-concepts/security-caveats` |
| 136 | +- [How It Works](/core-concepts/how-it-works) |
| 137 | +- [Security Caveats](/core-concepts/security-caveats) |
| 138 | +- [API Protection Guide](/guides/api-protection) |
0 commit comments