better-auth-nuxt 0.0.3 → 0.0.4
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
|
@@ -34,9 +34,9 @@ interface ModuleOptions {
|
|
|
34
34
|
* redirect options
|
|
35
35
|
*/
|
|
36
36
|
redirectOptions: {
|
|
37
|
-
redirectUserTo
|
|
38
|
-
redirectGuestTo
|
|
39
|
-
redirectUnauthorizedTo
|
|
37
|
+
redirectUserTo?: string;
|
|
38
|
+
redirectGuestTo?: string;
|
|
39
|
+
redirectUnauthorizedTo?: string;
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
42
|
interface ModulePublicRuntimeConfig {
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -156,8 +156,8 @@ const module = defineNuxtModule({
|
|
|
156
156
|
endpoint: "/api/auth/**",
|
|
157
157
|
serverConfigs: [],
|
|
158
158
|
redirectOptions: {
|
|
159
|
-
redirectUserTo: "/
|
|
160
|
-
redirectGuestTo: "/
|
|
159
|
+
redirectUserTo: "/auth/login",
|
|
160
|
+
redirectGuestTo: "/",
|
|
161
161
|
redirectUnauthorizedTo: "/401"
|
|
162
162
|
}
|
|
163
163
|
},
|
|
@@ -222,7 +222,6 @@ const module = defineNuxtModule({
|
|
|
222
222
|
continue;
|
|
223
223
|
}
|
|
224
224
|
for (const path of [...paths, ...pathsJS]) {
|
|
225
|
-
console.log("path", path);
|
|
226
225
|
if (fs.existsSync(path)) {
|
|
227
226
|
serverConfigs.push({
|
|
228
227
|
key: pascalCase(hash(path)),
|
|
@@ -274,7 +273,6 @@ const module = defineNuxtModule({
|
|
|
274
273
|
continue;
|
|
275
274
|
}
|
|
276
275
|
for (const path of [...paths, ...pathsJS]) {
|
|
277
|
-
console.log("path", path);
|
|
278
276
|
if (fs.existsSync(path)) {
|
|
279
277
|
clientConfigs.push({
|
|
280
278
|
key: pascalCase(hash(path)),
|
|
@@ -333,10 +331,48 @@ const module = defineNuxtModule({
|
|
|
333
331
|
}
|
|
334
332
|
]);
|
|
335
333
|
addRouteMiddleware({
|
|
336
|
-
name: "
|
|
337
|
-
path: resolver.resolve("./runtime/middleware/auth.
|
|
334
|
+
name: "auth",
|
|
335
|
+
path: resolver.resolve("./runtime/middleware/auth.ts"),
|
|
338
336
|
global: true
|
|
339
337
|
});
|
|
338
|
+
addTypeTemplate({
|
|
339
|
+
filename: "types/better-auth-middleware.d.ts",
|
|
340
|
+
getContents: () => {
|
|
341
|
+
return [
|
|
342
|
+
" type MiddlewareOptions = false | {",
|
|
343
|
+
" /**",
|
|
344
|
+
" * Only apply auth middleware to guest or user",
|
|
345
|
+
" */",
|
|
346
|
+
" only?:",
|
|
347
|
+
' | "guest"',
|
|
348
|
+
' | "user"',
|
|
349
|
+
' | "member"',
|
|
350
|
+
' | "admin"',
|
|
351
|
+
" /**",
|
|
352
|
+
" * Redirect authenticated user to this route",
|
|
353
|
+
" */",
|
|
354
|
+
" redirectUserTo?: string",
|
|
355
|
+
" /**",
|
|
356
|
+
" * Redirect guest to this route",
|
|
357
|
+
" */",
|
|
358
|
+
" redirectGuestTo?: string",
|
|
359
|
+
" redirectUnauthorizedTo?: string",
|
|
360
|
+
" }",
|
|
361
|
+
' declare module "#app" {',
|
|
362
|
+
" interface PageMeta {",
|
|
363
|
+
" auth?: MiddlewareOptions",
|
|
364
|
+
" }",
|
|
365
|
+
" }",
|
|
366
|
+
' declare module "vue-router" {',
|
|
367
|
+
" interface RouteMeta {",
|
|
368
|
+
" auth?: MiddlewareOptions",
|
|
369
|
+
" }",
|
|
370
|
+
" }",
|
|
371
|
+
"export {}"
|
|
372
|
+
].join("\n");
|
|
373
|
+
},
|
|
374
|
+
write: true
|
|
375
|
+
});
|
|
340
376
|
addPlugin(resolver.resolve("./runtime/plugin"));
|
|
341
377
|
}
|
|
342
378
|
});
|
|
@@ -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.4",
|
|
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;
|