1- # Nuxt Better Auth
1+ # @ onmax/nuxt-better-auth
22
33[ ![ npm version] [ npm-version-src ]] [ npm-version-href ]
44[ ![ npm downloads] [ npm-downloads-src ]] [ npm-downloads-href ]
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 [ NuxtHub] ( https://hub.nuxt.com ) . Provides route protection, session management, and role-based access .
8+ Nuxt module for [ Better Auth] ( https://better-auth.com ) with auto schema generation, route protection, and session management. Works with [ NuxtHub] ( https://hub.nuxt.com ) and future ` @nuxt/db ` .
99
1010## Features
1111
12- - NuxtHub Integration - Uses ` hub:db ` for database access
13- - Route Protection - Declarative access rules via ` routeRules `
14- - Session Management - Server and client plugins that sync auth state
15- - Role-Based Access - Support for ` admin ` , ` user ` , and custom roles
16- - Tier Gating - Generic tier system for subscription/premium features
17- - Auto-Imports - ` useUserSession ` , ` usePageAccess ` , ` requireUserSession ` , ` getUserSession `
12+ - ** Auto Schema Generation ** - Generates Drizzle schema from your better-auth plugins
13+ - ** Route Protection** - Declarative access rules via ` routeRules `
14+ - ** Session Management** - SSR-safe session handling with client hydration
15+ - ** Role-Based Access** - Support for ` admin ` , ` user ` , and custom roles
16+ - ** Auto-Imports ** - ` useUserSession ` , ` requireUserSession ` , ` getUserSession `
17+ - ** BetterAuthState Component ** - Ready-to-use Vue component with slots
1818
1919## Requirements
2020
21- - NuxtHub with database enabled (` hub: { database: true } ` )
21+ - NuxtHub with database enabled (` hub: { database: true } ` ) or future ` @nuxt/db `
2222
2323## Quick Start
2424
2525### 1. Install
2626
2727``` bash
28- pnpm add nuxt-better-auth better-auth drizzle-orm @nuxthub/core
28+ pnpm add @onmax/ nuxt-better-auth better-auth drizzle-orm @nuxthub/core
2929```
3030
3131### 2. Configure Nuxt
3232
3333``` ts
3434export default defineNuxtConfig ({
35- modules: [' @nuxthub/core' , ' nuxt-better-auth' ],
35+ modules: [' @nuxthub/core' , ' @onmax/ nuxt-better-auth' ],
3636
3737 hub: { database: true },
3838
@@ -43,7 +43,7 @@ export default defineNuxtConfig({
4343
4444 routeRules: {
4545 ' /app/**' : { auth: ' user' },
46- ' /admin/**' : { auth: ' user ' , requiresAdmin: true },
46+ ' /admin/**' : { auth: { role: ' admin ' } },
4747 ' /login' : { auth: ' guest' },
4848 },
4949})
@@ -55,15 +55,17 @@ Create `server/auth.config.ts`:
5555
5656``` ts
5757import { admin } from ' better-auth/plugins'
58- import { defineServerAuth } from ' nuxt-better-auth'
58+ import { defineServerAuth } from ' @onmax/ nuxt-better-auth'
5959
60- export default defineServerAuth (({ runtimeConfig , db }) => ({
60+ export default defineServerAuth (({ db }) => ({
6161 appName: ' My App' ,
6262 plugins: [admin ()],
6363 emailAndPassword: { enabled: true },
6464}))
6565```
6666
67+ > Schema is auto-generated from your plugins! No manual Drizzle schema needed.
68+
6769### 4. Create Client Config
6870
6971Create ` app/auth.client.ts ` :
@@ -101,23 +103,34 @@ declare module '#nuxt-better-auth' {
101103
102104| Option | Type | Description |
103105| --------| ------| -------------|
104- | ` auth ` | ` boolean \| 'guest' \| 'user' ` | Auth requirement |
105- | ` role ` | ` string \| string[] ` | Required role(s) |
106- | ` requiresAdmin ` | ` boolean ` | Shorthand for ` role: 'admin' ` |
107- | ` tier ` | ` string \| string[] ` | Required tier |
106+ | ` auth ` | ` false \| 'guest' \| 'user' \| { role: string } ` | Auth requirement |
107+
108+ Examples:
109+ - ` auth: false ` - Public (default)
110+ - ` auth: 'guest' ` - Only unauthenticated users
111+ - ` auth: 'user' ` - Any authenticated user
112+ - ` auth: { role: 'admin' } ` - Requires specific role
108113
109114## Composables
110115
111116### ` useUserSession() `
112117
113118``` ts
114- const { user, session, loggedIn, ready, client, signIn, signUp, signOut, fetchSession, updateUser } = useUserSession ()
119+ const { user, session, loggedIn, ready, client } = useUserSession ()
115120```
116121
117- ### ` usePageAccess() `
118-
119- ``` ts
120- const { hasAccess, showPaywall, paywallConfig } = usePageAccess ()
122+ ### ` <BetterAuthState> `
123+
124+ ``` vue
125+ <BetterAuthState>
126+ <template #default="{ loggedIn, user, signOut }">
127+ <p v-if="loggedIn">Welcome, {{ user?.name }}</p>
128+ <p v-else>Not logged in</p>
129+ </template>
130+ <template #placeholder>
131+ <p>Loading...</p>
132+ </template>
133+ </BetterAuthState>
121134```
122135
123136## Server Utils
@@ -154,14 +167,14 @@ pnpm dev
154167MIT
155168
156169<!-- Badges -->
157- [ npm-version-src ] : https://img.shields.io/npm/v/nuxt-better-auth/latest.svg?style=flat&colorA=020420&colorB=00DC82
158- [ npm-version-href ] : https://npmjs.com/package/nuxt-better-auth
170+ [ npm-version-src ] : https://img.shields.io/npm/v/@onmax/ nuxt-better-auth/latest.svg?style=flat&colorA=020420&colorB=00DC82
171+ [ npm-version-href ] : https://npmjs.com/package/@onmax/ nuxt-better-auth
159172
160- [ npm-downloads-src ] : https://img.shields.io/npm/dm/nuxt-better-auth.svg?style=flat&colorA=020420&colorB=00DC82
161- [ npm-downloads-href ] : https://npm.chart.dev/nuxt-better-auth
173+ [ npm-downloads-src ] : https://img.shields.io/npm/dm/@onmax/ nuxt-better-auth.svg?style=flat&colorA=020420&colorB=00DC82
174+ [ npm-downloads-href ] : https://npm.chart.dev/@onmax/ nuxt-better-auth
162175
163- [ license-src ] : https://img.shields.io/npm/l/nuxt-better-auth.svg?style=flat&colorA=020420&colorB=00DC82
164- [ license-href ] : https://npmjs.com/package/nuxt-better-auth
176+ [ license-src ] : https://img.shields.io/npm/l/@onmax/ nuxt-better-auth.svg?style=flat&colorA=020420&colorB=00DC82
177+ [ license-href ] : https://npmjs.com/package/@onmax/ nuxt-better-auth
165178
166179[ nuxt-src ] : https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js
167180[ nuxt-href ] : https://nuxt.com
0 commit comments