55[ ![ License] [ license-src ]] [ license-href ]
66[ ![ Nuxt] [ nuxt-src ]] [ nuxt-href ]
77
8- Nuxt module for [ Better Auth] ( https://better-auth.com ) integration with route protection, session management, and role-based access.
8+ Nuxt module for [ Better Auth] ( https://better-auth.com ) integration with [ NuxtHub ] ( https://hub.nuxt.com ) . Provides route protection, session management, and role-based access.
99
1010## Features
1111
12+ - NuxtHub Integration - Uses ` hub:db ` for database access
1213- Route Protection - Declarative access rules via ` routeRules `
1314- Session Management - Server and client plugins that sync auth state
1415- Role-Based Access - Support for ` admin ` , ` user ` , and custom roles
1516- Tier Gating - Generic tier system for subscription/premium features
1617- Auto-Imports - ` useUserSession ` , ` usePageAccess ` , ` requireUserSession ` , ` getUserSession `
1718
19+ ## Requirements
20+
21+ - NuxtHub with database enabled (` hub: { database: true } ` )
22+
1823## Quick Start
1924
2025### 1. Install
2126
2227``` bash
23- pnpm add nuxt-better-auth better-auth drizzle-orm
28+ pnpm add nuxt-better-auth better-auth drizzle-orm @nuxthub/core
29+ ```
30+
31+ ### 2. Configure Nuxt
32+
33+ ``` ts
34+ export default defineNuxtConfig ({
35+ modules: [' @nuxthub/core' , ' nuxt-better-auth' ],
36+
37+ hub: { database: true },
38+
39+ runtimeConfig: {
40+ betterAuthSecret: ' ' , // BETTER_AUTH_SECRET env var
41+ public: { siteUrl: ' http://localhost:3000' },
42+ },
43+
44+ routeRules: {
45+ ' /app/**' : { auth: ' user' },
46+ ' /admin/**' : { auth: ' user' , requiresAdmin: true },
47+ ' /login' : { auth: ' guest' },
48+ },
49+ })
2450```
2551
26- ### 2 . Create Server Config
52+ ### 3 . Create Server Config
2753
2854Create ` server/auth.config.ts ` :
2955
@@ -38,7 +64,7 @@ export default defineServerAuth(({ runtimeConfig, db }) => ({
3864}))
3965```
4066
41- ### 3 . Create Client Config
67+ ### 4 . Create Client Config
4268
4369Create ` app/auth.client.ts ` :
4470
@@ -56,7 +82,7 @@ export function createAppAuthClient(baseURL: string) {
5682export type AppAuthClient = ReturnType <typeof createAppAuthClient >
5783` ` `
5884
59- ### 4 . Add Type Extensions
85+ ### 5 . Add Type Extensions
6086
6187Create ` shared /types /auth .d .ts ` :
6288
@@ -71,36 +97,6 @@ declare module '#nuxt-better-auth' {
7197}
7298```
7399
74- ### 5. Configure Nuxt
75-
76- ``` ts
77- export default defineNuxtConfig ({
78- modules: [' nuxt-better-auth' ],
79-
80- runtimeConfig: {
81- betterAuthSecret: ' ' , // BETTER_AUTH_SECRET env var
82- public: { siteUrl: ' http://localhost:3000' },
83- },
84-
85- routeRules: {
86- ' /app/**' : { auth: ' user' },
87- ' /admin/**' : { auth: ' user' , requiresAdmin: true },
88- ' /login' : { auth: ' guest' },
89- },
90- })
91- ```
92-
93- ### 6. Setup Database
94-
95- Set the global database instance in a server plugin:
96-
97- ``` ts
98- // server/plugins/db.ts
99- export default defineNitroPlugin (() => {
100- ;(globalThis as any ).__nuxt_better_auth_db = yourDrizzleInstance
101- })
102- ```
103-
104100## Route Rules
105101
106102| Option | Type | Description |
0 commit comments