better-auth-nuxt 0.0.9 → 0.0.10-beta.19

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.
Files changed (60) hide show
  1. package/README.md +19 -270
  2. package/dist/module.d.mts +13 -40
  3. package/dist/module.json +8 -5
  4. package/dist/module.mjs +625 -358
  5. package/dist/runtime/adapters/convex.d.ts +111 -0
  6. package/dist/runtime/adapters/convex.js +213 -0
  7. package/dist/runtime/app/components/BetterAuthState.d.vue.ts +20 -0
  8. package/dist/runtime/app/components/BetterAuthState.vue +9 -0
  9. package/dist/runtime/app/components/BetterAuthState.vue.d.ts +20 -0
  10. package/dist/runtime/app/composables/useUserSession.d.ts +22 -0
  11. package/dist/runtime/app/composables/useUserSession.js +159 -0
  12. package/dist/runtime/app/middleware/auth.global.d.ts +13 -0
  13. package/dist/runtime/app/middleware/auth.global.js +37 -0
  14. package/dist/runtime/app/pages/__better-auth-devtools.d.vue.ts +3 -0
  15. package/dist/runtime/app/pages/__better-auth-devtools.vue +426 -0
  16. package/dist/runtime/app/pages/__better-auth-devtools.vue.d.ts +3 -0
  17. package/dist/runtime/app/plugins/session.client.d.ts +2 -0
  18. package/dist/runtime/app/plugins/session.client.js +15 -0
  19. package/dist/runtime/app/plugins/session.server.d.ts +2 -0
  20. package/dist/runtime/app/plugins/session.server.js +24 -0
  21. package/dist/runtime/config.d.ts +49 -0
  22. package/dist/runtime/config.js +11 -0
  23. package/dist/runtime/server/api/_better-auth/_schema.d.ts +16 -0
  24. package/dist/runtime/server/api/_better-auth/_schema.js +11 -0
  25. package/dist/runtime/server/api/_better-auth/accounts.get.d.ts +14 -0
  26. package/dist/runtime/server/api/_better-auth/accounts.get.js +28 -0
  27. package/dist/runtime/server/api/_better-auth/config.get.d.ts +35 -0
  28. package/dist/runtime/server/api/_better-auth/config.get.js +47 -0
  29. package/dist/runtime/server/api/_better-auth/sessions.delete.d.ts +4 -0
  30. package/dist/runtime/server/api/_better-auth/sessions.delete.js +22 -0
  31. package/dist/runtime/server/api/_better-auth/sessions.get.d.ts +14 -0
  32. package/dist/runtime/server/api/_better-auth/sessions.get.js +43 -0
  33. package/dist/runtime/server/api/_better-auth/users.get.d.ts +14 -0
  34. package/dist/runtime/server/api/_better-auth/users.get.js +34 -0
  35. package/dist/runtime/server/api/auth/[...all].d.ts +2 -0
  36. package/dist/runtime/server/api/auth/[...all].js +6 -0
  37. package/dist/runtime/server/middleware/route-access.d.ts +2 -0
  38. package/dist/runtime/server/middleware/route-access.js +29 -0
  39. package/dist/runtime/server/tsconfig.json +1 -1
  40. package/dist/runtime/server/utils/auth.d.ts +7 -0
  41. package/dist/runtime/server/utils/auth.js +81 -0
  42. package/dist/runtime/server/utils/session.d.ts +9 -0
  43. package/dist/runtime/server/utils/session.js +23 -0
  44. package/dist/runtime/types/augment.d.ts +42 -0
  45. package/dist/runtime/types/augment.js +0 -0
  46. package/dist/runtime/types.d.ts +23 -0
  47. package/dist/runtime/types.js +0 -0
  48. package/dist/runtime/utils/match-user.d.ts +2 -0
  49. package/dist/runtime/utils/match-user.js +13 -0
  50. package/dist/types.d.mts +8 -6
  51. package/package.json +82 -29
  52. package/LICENSE +0 -21
  53. package/dist/runtime/middleware/auth.d.ts +0 -2
  54. package/dist/runtime/middleware/auth.js +0 -31
  55. package/dist/runtime/plugin.d.ts +0 -2
  56. package/dist/runtime/plugin.js +0 -3
  57. package/dist/runtime/server/handler.d.ts +0 -2
  58. package/dist/runtime/server/handler.js +0 -5
  59. package/dist/runtime/server.d.ts +0 -6
  60. package/dist/runtime/server.js +0 -5
package/README.md CHANGED
@@ -1,278 +1,27 @@
1
- # Better Auth Nuxt
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/onmax/nuxt-better-auth/main/.github/og.png" alt="Nuxt Better Auth" width="100%">
3
+ <br>
4
+ <sub>Designed by <a href="https://github.com/HugoRCD">HugoRCD</a></sub>
5
+ </p>
2
6
 
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
+ <h1 align="center">@onmax/nuxt-better-auth</h1>
7
8
 
8
- A Nuxt module for simple, flexible authentication in your Nuxt applications.
9
+ <p align="center">Nuxt module for <a href="https://better-auth.com">Better Auth</a></p>
9
10
 
10
- - [✨ &nbsp;Release Notes](/CHANGELOG.md)
11
- <!-- - [🏀 Online playground](https://stackblitz.com/github/your-org/my-module?file=playground%2Fapp.vue) -->
12
- <!-- - [📖 &nbsp;Documentation](https://example.com) -->
11
+ <p align="center">
12
+ <a href="https://npmjs.com/package/@onmax/nuxt-better-auth"><img src="https://img.shields.io/npm/v/@onmax/nuxt-better-auth/latest.svg?style=flat&colorA=020420&colorB=00DC82" alt="npm version"></a>
13
+ <a href="https://npm.chart.dev/@onmax/nuxt-better-auth"><img src="https://img.shields.io/npm/dm/@onmax/nuxt-better-auth.svg?style=flat&colorA=020420&colorB=00DC82" alt="npm downloads"></a>
14
+ <a href="https://npmjs.com/package/@onmax/nuxt-better-auth"><img src="https://img.shields.io/npm/l/@onmax/nuxt-better-auth.svg?style=flat&colorA=020420&colorB=00DC82" alt="License"></a>
15
+ <a href="https://nuxt.com"><img src="https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js" alt="Nuxt"></a>
16
+ </p>
13
17
 
14
- ## Features
18
+ > [!WARNING]
19
+ > This library is a work in progress and not ready for production use.
15
20
 
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
21
+ ## Documentation
22
22
 
23
- ## Quick Setup
23
+ **[nuxt-better-auth.onmax.me](https://nuxt-better-auth.onmax.me/)**
24
24
 
25
- 1. Install the module:
25
+ ## License
26
26
 
27
- ```bash
28
- # Using npm
29
- npm install better-auth-nuxt better-auth
30
-
31
- # Using yarn
32
- yarn add better-auth-nuxt better-auth
33
-
34
- # Using pnpm
35
- pnpm add better-auth-nuxt better-auth
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
- }
130
- ```
131
-
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
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
- // server/my-auth.better-auth.ts
213
- import type { BetterAuthOptions } from 'better-auth'
214
-
215
- export default () => ({
216
- // Custom server-side auth configuration
217
- } satisfies BetterAuthOptions)
218
- ```
219
-
220
- ### Client Configuration
221
-
222
- Create a `*.better-auth-client.ts` file to configure client-side auth:
223
-
224
- ```ts
225
- // app/my-auth.better-auth-client.ts
226
- import type { ClientOptions } from 'better-auth'
227
-
228
- export default {
229
- // Custom client-side auth configuration
230
- } satisfies ClientOptions
231
- ```
232
-
233
- > **Note**: After adding or modifying configuration files, run `pnpm nuxt prepare` to ensure your changes are properly recognized by Nuxt.
234
-
235
- ## Contribution
236
-
237
- <details>
238
- <summary>Local development</summary>
239
-
240
- ```bash
241
- # Install dependencies
242
- pnpm install
243
-
244
- # Generate type stubs
245
- pnpm dev:prepare
246
-
247
- # Develop with the playground
248
- pnpm dev
249
-
250
- # Build the playground
251
- pnpm dev:build
252
-
253
- # Run ESLint
254
- pnpm lint
255
-
256
- # Run Vitest
257
- pnpm test
258
- pnpm test:watch
259
-
260
- # Release new version
261
- pnpm release
262
- ```
263
-
264
- </details>
265
-
266
-
267
- <!-- Badges -->
268
- [npm-version-src]: https://img.shields.io/npm/v/better-auth-nuxt/latest.svg?style=flat&colorA=020420&colorB=00DC82
269
- [npm-version-href]: https://npmjs.com/package/better-auth-nuxt
270
-
271
- [npm-downloads-src]: https://img.shields.io/npm/dm/better-auth-nuxt.svg?style=flat&colorA=020420&colorB=00DC82
272
- [npm-downloads-href]: https://npm.chart.dev/better-auth-nuxt
273
-
274
- [license-src]: https://img.shields.io/npm/l/better-auth-nuxt.svg?style=flat&colorA=020420&colorB=00DC82
275
- [license-href]: https://npmjs.com/package/better-auth-nuxt
276
-
277
- [nuxt-src]: https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js
278
- [nuxt-href]: https://nuxt.com
27
+ MIT
package/dist/module.d.mts CHANGED
@@ -1,47 +1,20 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
- import { ClientOptions, BetterAuthOptions } from 'better-auth';
2
+ import { BetterAuthModuleOptions } from '../dist/runtime/config.js';
3
+ export { BetterAuthModuleOptions, defineClientAuth, defineServerAuth } from '../dist/runtime/config.js';
4
+ import { BetterAuthOptions } from 'better-auth';
5
+ export { Auth, AuthMeta, AuthMode, AuthRouteRules, AuthSession, AuthUser, InferSession, InferUser, RequireSessionOptions, ServerAuthContext, UserMatch } from '../dist/runtime/types.js';
3
6
 
4
- interface ModuleServerOptions extends Pick<BetterAuthOptions, 'appName' | 'baseURL' | 'basePath' | 'secret'> {
5
- }
6
- interface ModuleClientOptions extends Pick<ClientOptions, 'baseURL' | 'basePath' | 'disableDefaultFetchPlugins'> {
7
- }
8
- interface ModuleOptions {
9
- /**
10
- * auth endpoint
11
- * @default 'api/auth/**'
12
- */
13
- endpoint: string;
14
- /**
15
- * @default ['*.better-auth']
16
- */
17
- serverConfigs?: string[];
18
- /**
19
- * @default ['*.better-auth-client']
20
- */
21
- clientConfigs?: string[];
22
- options: {
23
- /**
24
- * client options object or path to client setup script
25
- */
26
- client?: ModuleClientOptions;
7
+ declare module '@nuxt/schema' {
8
+ interface NuxtHooks {
27
9
  /**
28
- * server options object or path to server setup script
10
+ * Extend better-auth config with additional plugins or options.
11
+ * Called after user's auth.config.ts is loaded.
12
+ * @param config - Partial config to merge into the auth options
29
13
  */
30
- server?: ModuleServerOptions;
31
- };
32
- /**
33
- * redirect options
34
- */
35
- redirectOptions: {
36
- redirectUserTo?: string;
37
- redirectGuestTo?: string;
38
- redirectUnauthorizedTo?: string;
39
- };
40
- }
41
- interface ModulePublicRuntimeConfig {
42
- betterAuth: ModuleOptions;
14
+ 'better-auth:config:extend': (config: Partial<BetterAuthOptions>) => void | Promise<void>;
15
+ }
43
16
  }
44
- declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
17
+
18
+ declare const _default: _nuxt_schema.NuxtModule<BetterAuthModuleOptions, BetterAuthModuleOptions, false>;
45
19
 
46
20
  export { _default as default };
47
- export type { ModuleClientOptions, ModuleOptions, ModulePublicRuntimeConfig, ModuleServerOptions };
package/dist/module.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
- "name": "better-auth",
3
- "configKey": "betterAuth",
4
- "version": "0.0.9",
2
+ "name": "@onmax/nuxt-better-auth",
3
+ "version": "0.0.10-beta.19",
4
+ "configKey": "auth",
5
+ "compatibility": {
6
+ "nuxt": ">=3.0.0"
7
+ },
5
8
  "builder": {
6
- "@nuxt/module-builder": "1.0.1",
7
- "unbuild": "3.5.0"
9
+ "@nuxt/module-builder": "1.0.2",
10
+ "unbuild": "3.6.1"
8
11
  }
9
12
  }