better-auth-nuxt 0.0.3 → 0.0.5
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/dist/module.d.mts
CHANGED
|
@@ -6,7 +6,6 @@ interface ModuleServerOptions extends Pick<BetterAuthOptions, 'appName' | 'baseU
|
|
|
6
6
|
interface ModuleClientOptions extends Pick<ClientOptions, 'baseURL' | 'basePath' | 'disableDefaultFetchPlugins'> {
|
|
7
7
|
}
|
|
8
8
|
interface ModuleOptions {
|
|
9
|
-
baseUrl: string;
|
|
10
9
|
/**
|
|
11
10
|
* auth endpoint
|
|
12
11
|
* @default 'api/auth/**'
|
|
@@ -24,19 +23,19 @@ interface ModuleOptions {
|
|
|
24
23
|
/**
|
|
25
24
|
* client options object or path to client setup script
|
|
26
25
|
*/
|
|
27
|
-
client
|
|
26
|
+
client?: ModuleClientOptions;
|
|
28
27
|
/**
|
|
29
28
|
* server options object or path to server setup script
|
|
30
29
|
*/
|
|
31
|
-
server
|
|
30
|
+
server?: ModuleServerOptions;
|
|
32
31
|
};
|
|
33
32
|
/**
|
|
34
33
|
* redirect options
|
|
35
34
|
*/
|
|
36
35
|
redirectOptions: {
|
|
37
|
-
redirectUserTo
|
|
38
|
-
redirectGuestTo
|
|
39
|
-
redirectUnauthorizedTo
|
|
36
|
+
redirectUserTo?: string;
|
|
37
|
+
redirectGuestTo?: string;
|
|
38
|
+
redirectUnauthorizedTo?: string;
|
|
40
39
|
};
|
|
41
40
|
}
|
|
42
41
|
interface ModulePublicRuntimeConfig {
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -14,6 +14,11 @@ async function serverAuth({ options }) {
|
|
|
14
14
|
return `import ${config.key} from "${config.path}"`;
|
|
15
15
|
}),
|
|
16
16
|
"const betterAuthConfigs = mergeDeep({all: true})({},",
|
|
17
|
+
"{",
|
|
18
|
+
...options.moduleOptions?.options?.server ? Object.entries(options.moduleOptions.options.server).map(([key, value]) => {
|
|
19
|
+
return ` ${key}: ${JSON.stringify(value)},`;
|
|
20
|
+
}) : [],
|
|
21
|
+
"},",
|
|
17
22
|
...options.configs.map((config) => {
|
|
18
23
|
return `${config.key},`;
|
|
19
24
|
}),
|
|
@@ -52,6 +57,9 @@ async function useUserSession({ options }) {
|
|
|
52
57
|
"",
|
|
53
58
|
" const authClient = createAuthClient({",
|
|
54
59
|
" baseURL: url.origin,",
|
|
60
|
+
...options.moduleOptions?.options?.client ? Object.entries(options.moduleOptions.options.client).map(([key, value]) => {
|
|
61
|
+
return ` ${key}: ${JSON.stringify(value)},`;
|
|
62
|
+
}) : [],
|
|
55
63
|
" fetchOptions: {",
|
|
56
64
|
...options.configs.map((config) => {
|
|
57
65
|
return ` ...${config.key}?.fetchOptions || {},`;
|
|
@@ -156,8 +164,8 @@ const module = defineNuxtModule({
|
|
|
156
164
|
endpoint: "/api/auth/**",
|
|
157
165
|
serverConfigs: [],
|
|
158
166
|
redirectOptions: {
|
|
159
|
-
redirectUserTo: "/
|
|
160
|
-
redirectGuestTo: "/
|
|
167
|
+
redirectUserTo: "/auth/login",
|
|
168
|
+
redirectGuestTo: "/",
|
|
161
169
|
redirectUnauthorizedTo: "/401"
|
|
162
170
|
}
|
|
163
171
|
},
|
|
@@ -173,7 +181,6 @@ const module = defineNuxtModule({
|
|
|
173
181
|
logger.withTag("better-auth").error("Missing endpoint option");
|
|
174
182
|
}
|
|
175
183
|
nuxt.options.runtimeConfig.public.betterAuth = defu(nuxt.options.runtimeConfig.public.betterAuth, {
|
|
176
|
-
baseUrl: options.baseUrl,
|
|
177
184
|
endpoint: options.endpoint,
|
|
178
185
|
redirectOptions: options.redirectOptions
|
|
179
186
|
});
|
|
@@ -222,7 +229,6 @@ const module = defineNuxtModule({
|
|
|
222
229
|
continue;
|
|
223
230
|
}
|
|
224
231
|
for (const path of [...paths, ...pathsJS]) {
|
|
225
|
-
console.log("path", path);
|
|
226
232
|
if (fs.existsSync(path)) {
|
|
227
233
|
serverConfigs.push({
|
|
228
234
|
key: pascalCase(hash(path)),
|
|
@@ -234,7 +240,7 @@ const module = defineNuxtModule({
|
|
|
234
240
|
const server = registerTemplate({
|
|
235
241
|
filename: "better-auth/server.mjs",
|
|
236
242
|
getContents: serverAuth,
|
|
237
|
-
options: { configs: serverConfigs }
|
|
243
|
+
options: { configs: serverConfigs, moduleOptions: options }
|
|
238
244
|
});
|
|
239
245
|
addTypeTemplate({
|
|
240
246
|
filename: "better-auth/server.d.ts",
|
|
@@ -274,7 +280,6 @@ const module = defineNuxtModule({
|
|
|
274
280
|
continue;
|
|
275
281
|
}
|
|
276
282
|
for (const path of [...paths, ...pathsJS]) {
|
|
277
|
-
console.log("path", path);
|
|
278
283
|
if (fs.existsSync(path)) {
|
|
279
284
|
clientConfigs.push({
|
|
280
285
|
key: pascalCase(hash(path)),
|
|
@@ -286,7 +291,7 @@ const module = defineNuxtModule({
|
|
|
286
291
|
const client = registerTemplate({
|
|
287
292
|
filename: "better-auth/client.mjs",
|
|
288
293
|
getContents: useUserSession,
|
|
289
|
-
options: { configs: clientConfigs }
|
|
294
|
+
options: { configs: clientConfigs, moduleOptions: options }
|
|
290
295
|
});
|
|
291
296
|
addTypeTemplate({
|
|
292
297
|
filename: "better-auth/client.d.ts",
|
|
@@ -333,10 +338,48 @@ const module = defineNuxtModule({
|
|
|
333
338
|
}
|
|
334
339
|
]);
|
|
335
340
|
addRouteMiddleware({
|
|
336
|
-
name: "
|
|
337
|
-
path: resolver.resolve("./runtime/middleware/auth.
|
|
341
|
+
name: "auth",
|
|
342
|
+
path: resolver.resolve("./runtime/middleware/auth.ts"),
|
|
338
343
|
global: true
|
|
339
344
|
});
|
|
345
|
+
addTypeTemplate({
|
|
346
|
+
filename: "types/better-auth-middleware.d.ts",
|
|
347
|
+
getContents: () => {
|
|
348
|
+
return [
|
|
349
|
+
" type MiddlewareOptions = false | {",
|
|
350
|
+
" /**",
|
|
351
|
+
" * Only apply auth middleware to guest or user",
|
|
352
|
+
" */",
|
|
353
|
+
" only?:",
|
|
354
|
+
' | "guest"',
|
|
355
|
+
' | "user"',
|
|
356
|
+
' | "member"',
|
|
357
|
+
' | "admin"',
|
|
358
|
+
" /**",
|
|
359
|
+
" * Redirect authenticated user to this route",
|
|
360
|
+
" */",
|
|
361
|
+
" redirectUserTo?: string",
|
|
362
|
+
" /**",
|
|
363
|
+
" * Redirect guest to this route",
|
|
364
|
+
" */",
|
|
365
|
+
" redirectGuestTo?: string",
|
|
366
|
+
" redirectUnauthorizedTo?: string",
|
|
367
|
+
" }",
|
|
368
|
+
' declare module "#app" {',
|
|
369
|
+
" interface PageMeta {",
|
|
370
|
+
" auth?: MiddlewareOptions",
|
|
371
|
+
" }",
|
|
372
|
+
" }",
|
|
373
|
+
' declare module "vue-router" {',
|
|
374
|
+
" interface RouteMeta {",
|
|
375
|
+
" auth?: MiddlewareOptions",
|
|
376
|
+
" }",
|
|
377
|
+
" }",
|
|
378
|
+
"export {}"
|
|
379
|
+
].join("\n");
|
|
380
|
+
},
|
|
381
|
+
write: true
|
|
382
|
+
});
|
|
340
383
|
addPlugin(resolver.resolve("./runtime/plugin"));
|
|
341
384
|
}
|
|
342
385
|
});
|
|
@@ -6,16 +6,8 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
|
|
6
6
|
return;
|
|
7
7
|
}
|
|
8
8
|
const { loggedIn, options, fetchSession, session } = useUserSession();
|
|
9
|
-
const { only, redirectUserTo, redirectGuestTo, redirectUnauthorizedTo } = defu(to.meta
|
|
10
|
-
|
|
11
|
-
await fetchSession();
|
|
12
|
-
}
|
|
13
|
-
if (loggedIn.value && to.path.startsWith("/auth/")) {
|
|
14
|
-
if (to.path === redirectUserTo) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
return navigateTo(redirectUserTo);
|
|
18
|
-
}
|
|
9
|
+
const { only, redirectUserTo, redirectGuestTo, redirectUnauthorizedTo } = defu(to.meta.auth, options);
|
|
10
|
+
await fetchSession();
|
|
19
11
|
if (only === "guest" && loggedIn.value) {
|
|
20
12
|
if (to.path === redirectUserTo) {
|
|
21
13
|
return;
|
|
@@ -27,9 +19,13 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
|
|
27
19
|
return;
|
|
28
20
|
return navigateTo(redirectGuestTo);
|
|
29
21
|
}
|
|
30
|
-
if (only && only !== "guest" && session.value) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
22
|
+
if (only && only !== "guest" && loggedIn.value && session.value) {
|
|
23
|
+
const userRole = session.value.role || "user";
|
|
24
|
+
const requiredRoles = Array.isArray(only) ? only : [only];
|
|
25
|
+
if (!requiredRoles.includes(userRole) && !requiredRoles.includes("user")) {
|
|
26
|
+
if (to.path === redirectUnauthorizedTo)
|
|
27
|
+
return;
|
|
28
|
+
return navigateTo(redirectUnauthorizedTo ?? "/401");
|
|
29
|
+
}
|
|
34
30
|
}
|
|
35
31
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "better-auth-nuxt",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Better Auth Nuxt Module",
|
|
5
5
|
"repository": "productdevbook/better-auth-nuxt",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
"@nuxt/module-builder": "^1.0.1",
|
|
53
53
|
"@nuxt/schema": "^3.16.2",
|
|
54
54
|
"@nuxt/test-utils": "^3.17.2",
|
|
55
|
+
"@tailwindcss/vite": "^4.1.4",
|
|
55
56
|
"@types/better-sqlite3": "^7.6.13",
|
|
56
57
|
"@types/node": "latest",
|
|
57
58
|
"better-auth": "^1.2.7",
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
type MiddlewareOptions = false | {
|
|
2
|
-
/**
|
|
3
|
-
* Only apply auth middleware to guest or user
|
|
4
|
-
*/
|
|
5
|
-
only?: 'guest' | 'user' | 'member' | 'admin';
|
|
6
|
-
/**
|
|
7
|
-
* Redirect authenticated user to this route
|
|
8
|
-
*/
|
|
9
|
-
redirectUserTo?: string;
|
|
10
|
-
/**
|
|
11
|
-
* Redirect guest to this route
|
|
12
|
-
*/
|
|
13
|
-
redirectGuestTo?: string;
|
|
14
|
-
redirectUnauthorizedTo?: string;
|
|
15
|
-
};
|
|
16
|
-
declare module '#app' {
|
|
17
|
-
interface PageMeta {
|
|
18
|
-
auth?: MiddlewareOptions;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
declare module 'vue-router' {
|
|
22
|
-
interface RouteMeta {
|
|
23
|
-
auth?: MiddlewareOptions;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
declare const _default: import("#app").RouteMiddleware;
|
|
27
|
-
export default _default;
|