better-auth-nuxt 0.0.5 → 0.0.7

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 CHANGED
@@ -5,7 +5,7 @@
5
5
  [![License][license-src]][license-href]
6
6
  [![Nuxt][nuxt-src]][nuxt-href]
7
7
 
8
- My new Nuxt module for doing amazing things.
8
+ A Nuxt module for simple, flexible authentication in your Nuxt applications.
9
9
 
10
10
  - [✨  Release Notes](/CHANGELOG.md)
11
11
  <!-- - [🏀 Online playground](https://stackblitz.com/github/your-org/my-module?file=playground%2Fapp.vue) -->
@@ -13,49 +13,246 @@ My new Nuxt module for doing amazing things.
13
13
 
14
14
  ## Features
15
15
 
16
- <!-- Highlight some of the features your module provide here -->
17
- - &nbsp;Foo
18
- - 🚠 &nbsp;Bar
19
- - 🌲 &nbsp;Baz
16
+ - ⚙️ &nbsp;Auto-scanning of client and server configs for end-to-end TS support
17
+ - 🚀 &nbsp;Built-in authentication middleware
18
+ - 🔑 &nbsp;Multiple authentication strategies (username, email, ...)
19
+ - 🛡️ &nbsp;Role-based access control
20
+ - 🔄 &nbsp;Session management
21
+ - 🔒 &nbsp;Redirect handling for authenticated/unauthenticated users
20
22
 
21
23
  ## Quick Setup
22
24
 
23
- Install the module to your Nuxt application with one command:
25
+ 1. Install the module:
24
26
 
25
27
  ```bash
26
- npx nuxi module add my-module
28
+ # Using npm
29
+ npm install better-auth-nuxt
30
+
31
+ # Using yarn
32
+ yarn add better-auth-nuxt
33
+
34
+ # Using pnpm
35
+ pnpm add better-auth-nuxt
36
+ ```
37
+
38
+ 2. Add the module to your `nuxt.config.ts`:
39
+
40
+ ```ts
41
+ export default defineNuxtConfig({
42
+ modules: ['better-auth-nuxt'],
43
+
44
+ betterAuth: {
45
+ // Configure auth endpoints (default: '/api/auth/**')
46
+ endpoint: '/api/auth/**',
47
+
48
+ // Configure redirect paths
49
+ redirectOptions: {
50
+ redirectGuestTo: '/auth/login',
51
+ redirectUserTo: '/',
52
+ redirectUnauthorizedTo: '/401',
53
+ },
54
+
55
+ // Configure client and server options
56
+ options: {
57
+ client: {
58
+ basePath: '/api/auth',
59
+ // Optional: baseURL, disableDefaultFetchPlugins
60
+ },
61
+ server: {
62
+ appName: 'My Nuxt App',
63
+ // Optional: baseURL, basePath, secret
64
+ },
65
+ },
66
+ }
67
+ })
68
+ ```
69
+
70
+ 3. Use the module in your pages:
71
+
72
+ ```vue
73
+ <script setup>
74
+ // Protect route for authenticated users only
75
+ definePageMeta({
76
+ auth: {
77
+ only: 'user',
78
+ }
79
+ })
80
+
81
+ // Access auth functionality
82
+ const { loggedIn, user, signOut } = useUserSession()
83
+ </script>
84
+
85
+ <template>
86
+ <div v-if="loggedIn">
87
+ <h1>Welcome, {{ user?.name }}</h1>
88
+ <button @click="signOut()">Sign Out</button>
89
+ </div>
90
+ </template>
91
+ ```
92
+
93
+ ## Module Options
94
+
95
+ ### Auth Configuration
96
+
97
+ ```ts
98
+ interface ModuleOptions {
99
+ // Auth endpoint
100
+ endpoint: string
101
+
102
+ // Patterns to match auth configuration files
103
+ serverConfigs?: string[]
104
+ clientConfigs?: string[]
105
+
106
+ // Client and server options
107
+ options: {
108
+ client?: ModuleClientOptions
109
+ server?: ModuleServerOptions
110
+ }
111
+
112
+ // Redirect options
113
+ redirectOptions: {
114
+ redirectUserTo?: string
115
+ redirectGuestTo?: string
116
+ redirectUnauthorizedTo?: string
117
+ }
118
+ }
119
+ ```
120
+
121
+ ### Server Options
122
+
123
+ ```ts
124
+ interface ModuleServerOptions {
125
+ appName?: string // Application name
126
+ baseURL?: string // Base URL for the auth API
127
+ basePath?: string // Base path for the auth API
128
+ secret?: string // Secret for JWT/session encryption
129
+ }
27
130
  ```
28
131
 
29
- That's it! You can now use My Module in your Nuxt app ✨
132
+ ### Client Options
133
+
134
+ ```ts
135
+ interface ModuleClientOptions {
136
+ baseURL?: string // Base URL for the auth API
137
+ basePath?: string // Base path for the auth API
138
+ disableDefaultFetchPlugins?: boolean // Disable default fetch plugins
139
+ }
140
+ ```
141
+
142
+ ## API Reference
143
+
144
+ ### Client-side Composables
145
+
146
+ #### `useUserSession()`
147
+
148
+ Provides access to the authenticated user session and auth methods.
149
+
150
+ ```ts
151
+ const {
152
+ // State
153
+ loggedIn, // Ref<boolean> - Is the user logged in
154
+ user, // Ref<User> - Current user data
155
+ session, // Ref<Session> - Current session data
30
156
 
157
+ // Methods
158
+ fetchSession, // () => Promise<void> - Fetch current session
159
+ signIn: {
160
+ username, // (credentials) => Promise<void> - Sign in with username
161
+ email, // (credentials) => Promise<void> - Sign in with email
162
+ },
163
+ signUp: {
164
+ username, // (userData) => Promise<void> - Register with username
165
+ email, // (userData) => Promise<void> - Register with email
166
+ },
167
+ signOut, // (options?) => Promise<void> - Sign out the user
168
+
169
+ // Configuration
170
+ options, // Auth configuration options
171
+ } = useUserSession()
172
+ ```
173
+
174
+ ### Server-side Utilities
175
+
176
+ #### `useAuth()`
177
+
178
+ Access the auth instance on the server.
179
+
180
+ ```ts
181
+ // In API route handlers:
182
+ const auth = useAuth()
183
+ ```
184
+
185
+ ### Route Protection
186
+
187
+ Use the `auth` meta property to protect routes:
188
+
189
+ ```ts
190
+ definePageMeta({
191
+ auth: {
192
+ // Only allow specific roles
193
+ only: 'user' | 'admin' | 'guest' | ['user', 'admin'],
194
+
195
+ // Custom redirect paths (override global config)
196
+ redirectUserTo: '/dashboard',
197
+ redirectGuestTo: '/login',
198
+ redirectUnauthorizedTo: '/unauthorized',
199
+ }
200
+ })
201
+ ```
202
+
203
+ ## Configuration Files
204
+
205
+ You can create configuration files to customize authentication:
206
+
207
+ ### Server Configuration
208
+
209
+ Create a `*.better-auth.ts` file to configure server-side auth:
210
+
211
+ ```ts
212
+ // app/my-auth.better-auth.ts
213
+ export default {
214
+ // Custom server-side auth configuration
215
+ }
216
+ ```
217
+
218
+ ### Client Configuration
219
+
220
+ Create a `*.better-auth-client.ts` file to configure client-side auth:
221
+
222
+ ```ts
223
+ // app/my-auth.better-auth-client.ts
224
+ export default {
225
+ // Custom client-side auth configuration
226
+ }
227
+ ```
31
228
 
32
229
  ## Contribution
33
230
 
34
231
  <details>
35
232
  <summary>Local development</summary>
36
-
233
+
37
234
  ```bash
38
235
  # Install dependencies
39
- npm install
40
-
236
+ pnpm install
237
+
41
238
  # Generate type stubs
42
- npm run dev:prepare
43
-
239
+ pnpm dev:prepare
240
+
44
241
  # Develop with the playground
45
- npm run dev
46
-
242
+ pnpm dev
243
+
47
244
  # Build the playground
48
- npm run dev:build
49
-
245
+ pnpm dev:build
246
+
50
247
  # Run ESLint
51
- npm run lint
52
-
248
+ pnpm lint
249
+
53
250
  # Run Vitest
54
- npm run test
55
- npm run test:watch
56
-
251
+ pnpm test
252
+ pnpm test:watch
253
+
57
254
  # Release new version
58
- npm run release
255
+ pnpm release
59
256
  ```
60
257
 
61
258
  </details>
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "better-auth",
3
3
  "configKey": "betterAuth",
4
- "version": "0.0.5",
4
+ "version": "0.0.7",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.1",
7
7
  "unbuild": "3.5.0"
package/dist/module.mjs CHANGED
@@ -339,7 +339,7 @@ const module = defineNuxtModule({
339
339
  ]);
340
340
  addRouteMiddleware({
341
341
  name: "auth",
342
- path: resolver.resolve("./runtime/middleware/auth.ts"),
342
+ path: resolver.resolve("./runtime/middleware/auth"),
343
343
  global: true
344
344
  });
345
345
  addTypeTemplate({
@@ -1,2 +1,2 @@
1
- declare const _default: import("#app").RouteMiddleware;
1
+ declare const _default: import("nuxt/app").RouteMiddleware;
2
2
  export default _default;
@@ -1,2 +1,2 @@
1
- declare const _default: import("#app").Plugin<Record<string, unknown>> & import("#app").ObjectPlugin<Record<string, unknown>>;
1
+ declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
2
2
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-auth-nuxt",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Better Auth Nuxt Module",
5
5
  "repository": "productdevbook/better-auth-nuxt",
6
6
  "license": "MIT",
@@ -38,31 +38,31 @@
38
38
  "better-auth": ">=1.2.7"
39
39
  },
40
40
  "dependencies": {
41
- "@fastify/deepmerge": "^3.1.0",
42
- "@nuxt/kit": "^3.16.2",
43
- "defu": "^6.1.4",
44
- "ohash": "^2.0.11",
45
- "pathe": "^2.0.3",
46
- "scule": "^1.3.0",
47
- "tinyglobby": "^0.2.12"
41
+ "@fastify/deepmerge": "catalog:",
42
+ "@nuxt/kit": "catalog:",
43
+ "defu": "catalog:",
44
+ "ohash": "catalog:",
45
+ "pathe": "catalog:",
46
+ "scule": "catalog:",
47
+ "tinyglobby": "catalog:"
48
48
  },
49
49
  "devDependencies": {
50
- "@nuxt/devtools": "^2.4.0",
51
- "@nuxt/eslint-config": "^1.3.0",
52
- "@nuxt/module-builder": "^1.0.1",
53
- "@nuxt/schema": "^3.16.2",
54
- "@nuxt/test-utils": "^3.17.2",
55
- "@tailwindcss/vite": "^4.1.4",
56
- "@types/better-sqlite3": "^7.6.13",
50
+ "@nuxt/devtools": "catalog:",
51
+ "@nuxt/eslint-config": "catalog:",
52
+ "@nuxt/module-builder": "catalog:",
53
+ "@nuxt/schema": "catalog:",
54
+ "@nuxt/test-utils": "catalog:",
55
+ "@tailwindcss/vite": "catalog:",
56
+ "@types/better-sqlite3": "catalog:",
57
57
  "@types/node": "latest",
58
- "better-auth": "^1.2.7",
59
- "better-sqlite3": "^11.9.1",
60
- "changelogen": "^0.6.1",
61
- "eslint": "^9.24.0",
62
- "nuxt": "^3.16.2",
63
- "typescript": "~5.8.3",
64
- "vitest": "^3.1.1",
65
- "vue-tsc": "^2.2.8"
58
+ "better-auth": "catalog:",
59
+ "better-sqlite3": "catalog:",
60
+ "changelogen": "catalog:",
61
+ "eslint": "catalog:",
62
+ "nuxt": "catalog:",
63
+ "typescript": "catalog:",
64
+ "vitest": "catalog:",
65
+ "vue-tsc": "catalog:"
66
66
  },
67
- "packageManager": "pnpm@10.8.1"
67
+ "packageManager": "pnpm@10.10.0"
68
68
  }