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 +220 -23
- package/dist/module.json +1 -1
- package/dist/module.mjs +1 -1
- package/dist/runtime/middleware/auth.d.ts +1 -1
- package/dist/runtime/plugin.d.ts +1 -1
- package/package.json +24 -24
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,246 @@ 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
|
|
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
|
-
|
|
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
|
-
|
|
40
|
-
|
|
236
|
+
pnpm install
|
|
237
|
+
|
|
41
238
|
# Generate type stubs
|
|
42
|
-
|
|
43
|
-
|
|
239
|
+
pnpm dev:prepare
|
|
240
|
+
|
|
44
241
|
# Develop with the playground
|
|
45
|
-
|
|
46
|
-
|
|
242
|
+
pnpm dev
|
|
243
|
+
|
|
47
244
|
# Build the playground
|
|
48
|
-
|
|
49
|
-
|
|
245
|
+
pnpm dev:build
|
|
246
|
+
|
|
50
247
|
# Run ESLint
|
|
51
|
-
|
|
52
|
-
|
|
248
|
+
pnpm lint
|
|
249
|
+
|
|
53
250
|
# Run Vitest
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
251
|
+
pnpm test
|
|
252
|
+
pnpm test:watch
|
|
253
|
+
|
|
57
254
|
# Release new version
|
|
58
|
-
|
|
255
|
+
pnpm release
|
|
59
256
|
```
|
|
60
257
|
|
|
61
258
|
</details>
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("
|
|
1
|
+
declare const _default: import("nuxt/app").RouteMiddleware;
|
|
2
2
|
export default _default;
|
package/dist/runtime/plugin.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("
|
|
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.
|
|
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": "
|
|
42
|
-
"@nuxt/kit": "
|
|
43
|
-
"defu": "
|
|
44
|
-
"ohash": "
|
|
45
|
-
"pathe": "
|
|
46
|
-
"scule": "
|
|
47
|
-
"tinyglobby": "
|
|
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": "
|
|
51
|
-
"@nuxt/eslint-config": "
|
|
52
|
-
"@nuxt/module-builder": "
|
|
53
|
-
"@nuxt/schema": "
|
|
54
|
-
"@nuxt/test-utils": "
|
|
55
|
-
"@tailwindcss/vite": "
|
|
56
|
-
"@types/better-sqlite3": "
|
|
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": "
|
|
59
|
-
"better-sqlite3": "
|
|
60
|
-
"changelogen": "
|
|
61
|
-
"eslint": "
|
|
62
|
-
"nuxt": "
|
|
63
|
-
"typescript": "
|
|
64
|
-
"vitest": "
|
|
65
|
-
"vue-tsc": "
|
|
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.
|
|
67
|
+
"packageManager": "pnpm@10.10.0"
|
|
68
68
|
}
|