@onmax/nuxt-better-auth 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,180 @@
1
+ # @onmax/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) with auto schema generation, route protection, and session management. Works with [NuxtHub](https://hub.nuxt.com) and future `@nuxt/db`.
9
+
10
+ ## Features
11
+
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
18
+
19
+ ## Requirements
20
+
21
+ - NuxtHub with database enabled (`hub: { database: true }`) or future `@nuxt/db`
22
+
23
+ ## Quick Start
24
+
25
+ ### 1. Install
26
+
27
+ ```bash
28
+ pnpm add @onmax/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', '@onmax/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: { role: 'admin' } },
47
+ '/login': { auth: 'guest' },
48
+ },
49
+ })
50
+ ```
51
+
52
+ ### 3. Create Server Config
53
+
54
+ Create `server/auth.config.ts`:
55
+
56
+ ```ts
57
+ import { admin } from 'better-auth/plugins'
58
+ import { defineServerAuth } from '@onmax/nuxt-better-auth'
59
+
60
+ export default defineServerAuth(({ db }) => ({
61
+ appName: 'My App',
62
+ plugins: [admin()],
63
+ emailAndPassword: { enabled: true },
64
+ }))
65
+ ```
66
+
67
+ > Schema is auto-generated from your plugins! No manual Drizzle schema needed.
68
+
69
+ ### 4. Create Client Config
70
+
71
+ Create `app/auth.client.ts`:
72
+
73
+ ```ts
74
+ import { adminClient } from 'better-auth/client/plugins'
75
+ import { createAuthClient } from 'better-auth/vue'
76
+
77
+ export function createAppAuthClient(baseURL: string) {
78
+ return createAuthClient({
79
+ baseURL,
80
+ plugins: [adminClient()],
81
+ })
82
+ }
83
+
84
+ export type AppAuthClient = ReturnType<typeof createAppAuthClient>
85
+ ```
86
+
87
+ ### 5. Add Type Extensions
88
+
89
+ Create `shared/types/auth.d.ts`:
90
+
91
+ ```ts
92
+ import '#nuxt-better-auth'
93
+
94
+ declare module '#nuxt-better-auth' {
95
+ interface AuthUser {
96
+ role?: string | null
97
+ banned?: boolean | null
98
+ }
99
+ }
100
+ ```
101
+
102
+ ## Route Rules
103
+
104
+ | Option | Type | Description |
105
+ |--------|------|-------------|
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
113
+
114
+ ## Composables
115
+
116
+ ### `useUserSession()`
117
+
118
+ ```ts
119
+ const { user, session, loggedIn, ready, client } = useUserSession()
120
+ ```
121
+
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>
134
+ ```
135
+
136
+ ## Server Utils
137
+
138
+ ```ts
139
+ // Require auth
140
+ const { user, session } = await requireUserSession(event)
141
+
142
+ // Require admin
143
+ const { user } = await requireUserSession(event, { role: 'admin' })
144
+
145
+ // Optional session
146
+ const session = await getUserSession(event)
147
+ ```
148
+
149
+ ## Module Aliases
150
+
151
+ | Alias | Points To |
152
+ |-------|-----------|
153
+ | `#auth/server` | `server/auth.config.ts` |
154
+ | `#auth/client` | `app/auth.client.ts` |
155
+ | `#nuxt-better-auth` | Module type augmentation |
156
+
157
+ ## Development
158
+
159
+ ```bash
160
+ pnpm install
161
+ pnpm dev:prepare
162
+ pnpm dev
163
+ ```
164
+
165
+ ## License
166
+
167
+ MIT
168
+
169
+ <!-- Badges -->
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
172
+
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
175
+
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
178
+
179
+ [nuxt-src]: https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js
180
+ [nuxt-href]: https://nuxt.com
@@ -0,0 +1,2 @@
1
+ export * from "/home/maxi/nuxt/better-auth/src/module.js";
2
+ export { default } from "/home/maxi/nuxt/better-auth/src/module.js";
@@ -0,0 +1,2 @@
1
+ export * from "/home/maxi/nuxt/better-auth/src/module.js";
2
+ export { default } from "/home/maxi/nuxt/better-auth/src/module.js";
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@onmax/nuxt-better-auth",
3
+ "configKey": "auth",
4
+ "compatibility": {
5
+ "nuxt": ">=3.0.0"
6
+ },
7
+ "version": "0.0.1",
8
+ "builder": {
9
+ "@nuxt/module-builder": "1.0.2",
10
+ "unbuild": "3.6.1"
11
+ }
12
+ }
@@ -0,0 +1,20 @@
1
+ import { createJiti } from "file:///home/maxi/nuxt/better-auth/node_modules/.pnpm/jiti@2.6.1/node_modules/jiti/lib/jiti.mjs";
2
+
3
+ const jiti = createJiti(import.meta.url, {
4
+ "interopDefault": true,
5
+ "alias": {
6
+ "@onmax/nuxt-better-auth": "/home/maxi/nuxt/better-auth"
7
+ },
8
+ "transformOptions": {
9
+ "babel": {
10
+ "plugins": []
11
+ }
12
+ }
13
+ })
14
+
15
+ /** @type {import("/home/maxi/nuxt/better-auth/src/module.js")} */
16
+ const _module = await jiti.import("/home/maxi/nuxt/better-auth/src/module.ts");
17
+
18
+ export default _module?.default ?? _module;
19
+ export const defineClientAuth = _module.defineClientAuth;
20
+ export const defineServerAuth = _module.defineServerAuth;
@@ -0,0 +1,13 @@
1
+ import type { ModuleHooks, ModuleRuntimeHooks, ModuleRuntimeConfig, ModulePublicRuntimeConfig } from './module.mjs'
2
+
3
+ declare module '#app' {
4
+ interface RuntimeNuxtHooks extends ModuleRuntimeHooks {}
5
+ }
6
+
7
+ declare module '@nuxt/schema' {
8
+ interface NuxtHooks extends ModuleHooks {}
9
+ interface RuntimeConfig extends ModuleRuntimeConfig {}
10
+ interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
11
+ }
12
+
13
+ export * from "./module.mjs"
package/package.json ADDED
@@ -0,0 +1,85 @@
1
+ {
2
+ "name": "@onmax/nuxt-better-auth",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "description": "Nuxt module for Better Auth integration with NuxtHub, route protection, session management, and role-based access",
6
+ "license": "MIT",
7
+ "author": "onmax",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/onmax/nuxt-better-auth"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/types.d.mts",
15
+ "import": "./dist/module.mjs"
16
+ }
17
+ },
18
+ "main": "./dist/module.mjs",
19
+ "typesVersions": {
20
+ "*": {
21
+ ".": [
22
+ "./dist/types.d.mts"
23
+ ]
24
+ }
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "scripts": {
30
+ "prepack": "nuxt-module-build build",
31
+ "dev": "pnpm dev:prepare && nuxi dev playground",
32
+ "dev:build": "nuxi build playground",
33
+ "dev:prepare": "nuxt-module-build build --stub && nuxi prepare playground",
34
+ "prepare": "nuxt-module-build build --stub && nuxi prepare playground",
35
+ "release": "pnpm lint && pnpm test && pnpm prepack && changelogen --release && npm publish && git push --follow-tags",
36
+ "lint": "eslint .",
37
+ "lint:fix": "eslint . --fix",
38
+ "typecheck": "vue-tsc --noEmit",
39
+ "typecheck:playground": "cd playground && vue-tsc --noEmit",
40
+ "test": "vitest run",
41
+ "test:watch": "vitest watch"
42
+ },
43
+ "peerDependencies": {
44
+ "@nuxthub/core": ">=0.10.0",
45
+ "better-auth": ">=1.0.0",
46
+ "drizzle-orm": ">=0.30.0"
47
+ },
48
+ "dependencies": {
49
+ "@nuxt/kit": "^4.2.2",
50
+ "defu": "^6.1.4",
51
+ "jiti": "^2.4.2",
52
+ "pathe": "^2.0.3",
53
+ "radix3": "^1.1.2"
54
+ },
55
+ "devDependencies": {
56
+ "@antfu/eslint-config": "^4.12.0",
57
+ "@better-auth/cli": "^1.4.6",
58
+ "@libsql/client": "^0.15.15",
59
+ "@nuxt/devtools": "^3.1.1",
60
+ "@nuxt/module-builder": "^1.0.2",
61
+ "@nuxt/schema": "^4.2.2",
62
+ "@nuxt/test-utils": "^3.21.0",
63
+ "@nuxthub/core": "^0.10.0",
64
+ "@types/better-sqlite3": "^7.6.13",
65
+ "@types/node": "latest",
66
+ "better-auth": "^1.2.8",
67
+ "better-sqlite3": "^11.9.1",
68
+ "changelogen": "^0.6.2",
69
+ "consola": "^3.4.2",
70
+ "drizzle-kit": "^0.31.8",
71
+ "drizzle-orm": "^0.38.4",
72
+ "eslint": "^9.39.1",
73
+ "nuxt": "^4.2.2",
74
+ "typescript": "~5.9.3",
75
+ "vitest": "^4.0.15",
76
+ "vue-tsc": "^3.1.7"
77
+ },
78
+ "pnpm": {
79
+ "onlyBuiltDependencies": [
80
+ "better-sqlite3",
81
+ "esbuild",
82
+ "@parcel/watcher"
83
+ ]
84
+ }
85
+ }