|
| 1 | +# Nuxt Better Auth |
| 2 | + |
| 3 | +[![npm version][npm-version-src]][npm-version-href] |
| 4 | +[![npm downloads][npm-downloads-src]][npm-downloads-href] |
| 5 | +[![License][license-src]][license-href] |
| 6 | +[![Nuxt][nuxt-src]][nuxt-href] |
| 7 | + |
| 8 | +Nuxt module for [Better Auth](https://better-auth.com) integration with route protection, session management, and role-based access. |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- Route Protection - Declarative access rules via `routeRules` |
| 13 | +- Session Management - Server and client plugins that sync auth state |
| 14 | +- Role-Based Access - Support for `admin`, `user`, and custom roles |
| 15 | +- Tier Gating - Generic tier system for subscription/premium features |
| 16 | +- Auto-Imports - `useUserSession`, `usePageAccess`, `requireUserSession`, `getUserSession` |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +### 1. Install |
| 21 | + |
| 22 | +```bash |
| 23 | +pnpm add nuxt-better-auth better-auth drizzle-orm |
| 24 | +``` |
| 25 | + |
| 26 | +### 2. Create Server Config |
| 27 | + |
| 28 | +Create `server/auth.config.ts`: |
| 29 | + |
| 30 | +```ts |
| 31 | +import { admin } from 'better-auth/plugins' |
| 32 | +import { defineServerAuth } from 'nuxt-better-auth' |
| 33 | + |
| 34 | +export default defineServerAuth(({ runtimeConfig, db }) => ({ |
| 35 | + appName: 'My App', |
| 36 | + plugins: [admin()], |
| 37 | + emailAndPassword: { enabled: true }, |
| 38 | +})) |
| 39 | +``` |
| 40 | + |
| 41 | +### 3. Create Client Config |
| 42 | + |
| 43 | +Create `app/auth.client.ts`: |
| 44 | + |
| 45 | +```ts |
| 46 | +import { adminClient } from 'better-auth/client/plugins' |
| 47 | +import { createAuthClient } from 'better-auth/vue' |
| 48 | + |
| 49 | +export function createAppAuthClient(baseURL: string) { |
| 50 | + return createAuthClient({ |
| 51 | + baseURL, |
| 52 | + plugins: [adminClient()], |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +export type AppAuthClient = ReturnType<typeof createAppAuthClient> |
| 57 | +``` |
| 58 | +
|
| 59 | +### 4. Add Type Extensions |
| 60 | +
|
| 61 | +Create `shared/types/auth.d.ts`: |
| 62 | +
|
| 63 | +```ts |
| 64 | +import '#nuxt-better-auth' |
| 65 | + |
| 66 | +declare module '#nuxt-better-auth' { |
| 67 | + interface AuthUser { |
| 68 | + role?: string | null |
| 69 | + banned?: boolean | null |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 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 | + |
| 104 | +## Route Rules |
| 105 | + |
| 106 | +| Option | Type | Description | |
| 107 | +|--------|------|-------------| |
| 108 | +| `auth` | `boolean \| 'guest' \| 'user'` | Auth requirement | |
| 109 | +| `role` | `string \| string[]` | Required role(s) | |
| 110 | +| `requiresAdmin` | `boolean` | Shorthand for `role: 'admin'` | |
| 111 | +| `tier` | `string \| string[]` | Required tier | |
| 112 | + |
| 113 | +## Composables |
| 114 | + |
| 115 | +### `useUserSession()` |
| 116 | + |
| 117 | +```ts |
| 118 | +const { user, session, loggedIn, ready, client, signIn, signUp, signOut, fetchSession, updateUser } = useUserSession() |
| 119 | +``` |
| 120 | + |
| 121 | +### `usePageAccess()` |
| 122 | + |
| 123 | +```ts |
| 124 | +const { hasAccess, showPaywall, paywallConfig } = usePageAccess() |
| 125 | +``` |
| 126 | + |
| 127 | +## Server Utils |
| 128 | + |
| 129 | +```ts |
| 130 | +// Require auth |
| 131 | +const { user, session } = await requireUserSession(event) |
| 132 | + |
| 133 | +// Require admin |
| 134 | +const { user } = await requireUserSession(event, { role: 'admin' }) |
| 135 | + |
| 136 | +// Optional session |
| 137 | +const session = await getUserSession(event) |
| 138 | +``` |
| 139 | + |
| 140 | +## Module Aliases |
| 141 | + |
| 142 | +| Alias | Points To | |
| 143 | +|-------|-----------| |
| 144 | +| `#auth/server` | `server/auth.config.ts` | |
| 145 | +| `#auth/client` | `app/auth.client.ts` | |
| 146 | +| `#nuxt-better-auth` | Module type augmentation | |
| 147 | + |
| 148 | +## Development |
| 149 | + |
| 150 | +```bash |
| 151 | +pnpm install |
| 152 | +pnpm dev:prepare |
| 153 | +pnpm dev |
| 154 | +``` |
| 155 | + |
| 156 | +## License |
| 157 | + |
| 158 | +MIT |
| 159 | + |
| 160 | +<!-- Badges --> |
| 161 | +[npm-version-src]: https://img.shields.io/npm/v/nuxt-better-auth/latest.svg?style=flat&colorA=020420&colorB=00DC82 |
| 162 | +[npm-version-href]: https://npmjs.com/package/nuxt-better-auth |
| 163 | + |
| 164 | +[npm-downloads-src]: https://img.shields.io/npm/dm/nuxt-better-auth.svg?style=flat&colorA=020420&colorB=00DC82 |
| 165 | +[npm-downloads-href]: https://npm.chart.dev/nuxt-better-auth |
| 166 | + |
| 167 | +[license-src]: https://img.shields.io/npm/l/nuxt-better-auth.svg?style=flat&colorA=020420&colorB=00DC82 |
| 168 | +[license-href]: https://npmjs.com/package/nuxt-better-auth |
| 169 | + |
| 170 | +[nuxt-src]: https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js |
| 171 | +[nuxt-href]: https://nuxt.com |
0 commit comments