better-auth-nuxt 0.0.6 → 0.0.8
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 +226 -23
- package/dist/module.json +1 -1
- package/dist/runtime/server/handler.d.ts +1 -1
- package/package.json +20 -22
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
|
-
|
|
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,252 @@ My new Nuxt module for doing amazing things.
|
|
|
13
13
|
|
|
14
14
|
## Features
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
16
|
+
- ⚙️ Auto-scanning of client and server configs for end-to-end TS support
|
|
17
|
+
- 🚀 Built-in authentication middleware
|
|
18
|
+
- 🔑 Multiple authentication strategies (username, email, ...)
|
|
19
|
+
- 🛡️ Role-based access control
|
|
20
|
+
- 🔄 Session management
|
|
21
|
+
- 🔒 Redirect handling for authenticated/unauthenticated users
|
|
20
22
|
|
|
21
23
|
## Quick Setup
|
|
22
24
|
|
|
23
|
-
Install the module
|
|
25
|
+
1. Install the module:
|
|
24
26
|
|
|
25
27
|
```bash
|
|
26
|
-
|
|
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()
|
|
27
183
|
```
|
|
28
184
|
|
|
29
|
-
|
|
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:
|
|
30
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.
|
|
31
234
|
|
|
32
235
|
## Contribution
|
|
33
236
|
|
|
34
237
|
<details>
|
|
35
238
|
<summary>Local development</summary>
|
|
36
|
-
|
|
239
|
+
|
|
37
240
|
```bash
|
|
38
241
|
# Install dependencies
|
|
39
|
-
|
|
40
|
-
|
|
242
|
+
pnpm install
|
|
243
|
+
|
|
41
244
|
# Generate type stubs
|
|
42
|
-
|
|
43
|
-
|
|
245
|
+
pnpm dev:prepare
|
|
246
|
+
|
|
44
247
|
# Develop with the playground
|
|
45
|
-
|
|
46
|
-
|
|
248
|
+
pnpm dev
|
|
249
|
+
|
|
47
250
|
# Build the playground
|
|
48
|
-
|
|
49
|
-
|
|
251
|
+
pnpm dev:build
|
|
252
|
+
|
|
50
253
|
# Run ESLint
|
|
51
|
-
|
|
52
|
-
|
|
254
|
+
pnpm lint
|
|
255
|
+
|
|
53
256
|
# Run Vitest
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
257
|
+
pnpm test
|
|
258
|
+
pnpm test:watch
|
|
259
|
+
|
|
57
260
|
# Release new version
|
|
58
|
-
|
|
261
|
+
pnpm release
|
|
59
262
|
```
|
|
60
263
|
|
|
61
264
|
</details>
|
package/dist/module.json
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest,
|
|
1
|
+
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<Response>>;
|
|
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.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Better Auth Nuxt Module",
|
|
5
5
|
"repository": "productdevbook/better-auth-nuxt",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,35 +22,23 @@
|
|
|
22
22
|
"files": [
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
|
-
"scripts": {
|
|
26
|
-
"prepack": "nuxt-module-build build",
|
|
27
|
-
"dev": "nuxi dev playground",
|
|
28
|
-
"dev:build": "nuxi build playground",
|
|
29
|
-
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
30
|
-
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
|
|
31
|
-
"lint": "eslint .",
|
|
32
|
-
"lint:fix": "eslint . --fix",
|
|
33
|
-
"test": "vitest run",
|
|
34
|
-
"test:watch": "vitest watch",
|
|
35
|
-
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
|
|
36
|
-
},
|
|
37
25
|
"peerDependencies": {
|
|
38
26
|
"better-auth": ">=1.2.7"
|
|
39
27
|
},
|
|
40
28
|
"dependencies": {
|
|
41
29
|
"@fastify/deepmerge": "^3.1.0",
|
|
42
|
-
"@nuxt/kit": "^3.
|
|
30
|
+
"@nuxt/kit": "^3.17.0",
|
|
43
31
|
"defu": "^6.1.4",
|
|
44
32
|
"ohash": "^2.0.11",
|
|
45
33
|
"pathe": "^2.0.3",
|
|
46
34
|
"scule": "^1.3.0",
|
|
47
|
-
"tinyglobby": "^0.2.
|
|
35
|
+
"tinyglobby": "^0.2.13"
|
|
48
36
|
},
|
|
49
37
|
"devDependencies": {
|
|
50
38
|
"@nuxt/devtools": "^2.4.0",
|
|
51
39
|
"@nuxt/eslint-config": "^1.3.0",
|
|
52
40
|
"@nuxt/module-builder": "^1.0.1",
|
|
53
|
-
"@nuxt/schema": "^3.
|
|
41
|
+
"@nuxt/schema": "^3.17.0",
|
|
54
42
|
"@nuxt/test-utils": "^3.17.2",
|
|
55
43
|
"@tailwindcss/vite": "^4.1.4",
|
|
56
44
|
"@types/better-sqlite3": "^7.6.13",
|
|
@@ -58,11 +46,21 @@
|
|
|
58
46
|
"better-auth": "^1.2.7",
|
|
59
47
|
"better-sqlite3": "^11.9.1",
|
|
60
48
|
"changelogen": "^0.6.1",
|
|
61
|
-
"eslint": "^9.
|
|
62
|
-
"nuxt": "^3.
|
|
49
|
+
"eslint": "^9.25.1",
|
|
50
|
+
"nuxt": "^3.17.0",
|
|
63
51
|
"typescript": "~5.8.3",
|
|
64
|
-
"vitest": "^3.1.
|
|
65
|
-
"vue-tsc": "^2.2.
|
|
52
|
+
"vitest": "^3.1.2",
|
|
53
|
+
"vue-tsc": "^2.2.10"
|
|
66
54
|
},
|
|
67
|
-
"
|
|
68
|
-
|
|
55
|
+
"scripts": {
|
|
56
|
+
"dev": "nuxi dev playground",
|
|
57
|
+
"dev:build": "nuxi build playground",
|
|
58
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
59
|
+
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && pnpm publish --no-git-checks --access public && git push --follow-tags",
|
|
60
|
+
"lint": "eslint .",
|
|
61
|
+
"lint:fix": "eslint . --fix",
|
|
62
|
+
"test": "vitest run",
|
|
63
|
+
"test:watch": "vitest watch",
|
|
64
|
+
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
|
|
65
|
+
}
|
|
66
|
+
}
|