Skip to content

Commit 1dd6be6

Browse files
committed
rename to @onmax/nuxt-better-auth
1 parent 4a4b385 commit 1dd6be6

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

README.md

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
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
3434
export 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
5757
import { 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

6971
Create `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
154167
MIT
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

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{
2-
"name": "nuxt-better-auth",
2+
"name": "@onmax/nuxt-better-auth",
33
"type": "module",
44
"version": "0.1.0",
55
"description": "Nuxt module for Better Auth integration with NuxtHub, route protection, session management, and role-based access",
66
"license": "MIT",
7-
"repository": "your-org/nuxt-better-auth",
7+
"author": "onmax",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/onmax/nuxt-better-auth"
11+
},
812
"exports": {
913
".": {
1014
"types": "./dist/types.d.mts",

src/module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { generateDrizzleSchema, loadUserAuthConfig } from './schema-generator'
1111
export interface BetterAuthModuleOptions {}
1212

1313
export default defineNuxtModule<BetterAuthModuleOptions>({
14-
meta: { name: 'nuxt-better-auth', configKey: 'auth', compatibility: { nuxt: '>=3.0.0' } },
14+
meta: { name: '@onmax/nuxt-better-auth', configKey: 'auth', compatibility: { nuxt: '>=3.0.0' } },
1515
defaults: {},
1616
async setup(_options, nuxt) {
1717
const resolver = createResolver(import.meta.url)
@@ -24,9 +24,9 @@ export default defineNuxtModule<BetterAuthModuleOptions>({
2424
const clientConfigExists = existsSync(`${clientConfigPath}.ts`) || existsSync(`${clientConfigPath}.js`)
2525

2626
if (!serverConfigExists)
27-
throw new Error('[nuxt-better-auth] Missing server/auth.config.ts - create with defineServerAuth()')
27+
throw new Error('[@onmax/nuxt-better-auth] Missing server/auth.config.ts - create with defineServerAuth()')
2828
if (!clientConfigExists)
29-
throw new Error('[nuxt-better-auth] Missing app/auth.client.ts - export createAppAuthClient()')
29+
throw new Error('[@onmax/nuxt-better-auth] Missing app/auth.client.ts - export createAppAuthClient()')
3030

3131
// Register #nuxt-better-auth alias for type augmentation
3232
nuxt.options.alias['#nuxt-better-auth'] = resolver.resolve('./runtime/types/augment')

src/schema-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export async function loadUserAuthConfig(configPath: string): Promise<Partial<Be
146146
return {}
147147
}
148148
catch (error) {
149-
console.warn('[nuxt-better-auth] Failed to load auth config for schema generation:', error)
149+
console.warn('[@onmax/nuxt-better-auth] Failed to load auth config for schema generation:', error)
150150
return {}
151151
}
152152
}

0 commit comments

Comments
 (0)