@sfxcode/nuxt-ui-mongocamp 1.1.2 → 1.1.3

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
@@ -1,8 +1,34 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
2
 
3
3
  interface ModuleOptions {
4
+ useGlobalAuthMiddleware?: boolean;
5
+ /**
6
+ * Path the global auth middleware navigates to when a route is not allowed
7
+ * (not logged in, or logged in without the required role) — e.g. '/login'.
8
+ * Always treated as allowed by the middleware itself, so it can never cause a redirect loop.
9
+ */
10
+ notAllowedPath?: string;
11
+ /**
12
+ * Roles with management access
13
+ */
14
+ managerRoles: string[];
15
+ /**
16
+ * Routes that can only be accessed if the user is logged in
17
+ */
18
+ securedRouteParts: string[];
19
+ /**
20
+ * Routes that can only be accessed by manager users
21
+ */
22
+ managementRouteParts: string[];
23
+ /**
24
+ * Routes that can only be accessed by admin users
25
+ */
26
+ adminRouteParts: string[];
27
+ }
28
+ interface ModulePublicRuntimeConfig {
29
+ nuxtUiMongocampOptions: ModuleOptions;
4
30
  }
5
31
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
6
32
 
7
33
  export { _default as default };
8
- export type { ModuleOptions };
34
+ export type { ModuleOptions, ModulePublicRuntimeConfig };
package/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": ">=3.16.0"
6
6
  },
7
- "version": "1.1.2",
7
+ "version": "1.1.3",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.2",
10
10
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { defineNuxtModule, createResolver, addPlugin, addComponentsDir, addImportsDir } from '@nuxt/kit';
2
+ import { defu } from 'defu';
2
3
 
3
4
  const module$1 = defineNuxtModule({
4
5
  meta: {
@@ -9,7 +10,14 @@ const module$1 = defineNuxtModule({
9
10
  }
10
11
  },
11
12
  // Default configuration options of the Nuxt module
12
- defaults: {},
13
+ defaults: {
14
+ useGlobalAuthMiddleware: false,
15
+ notAllowedPath: "/",
16
+ managerRoles: [],
17
+ securedRouteParts: [],
18
+ managementRouteParts: [],
19
+ adminRouteParts: []
20
+ },
13
21
  moduleDependencies: {
14
22
  "@nuxt/ui": {},
15
23
  "unocss-nuxt-ui": {},
@@ -23,7 +31,11 @@ const module$1 = defineNuxtModule({
23
31
  }
24
32
  }
25
33
  },
26
- setup(_options, _nuxt) {
34
+ setup(options, nuxt) {
35
+ nuxt.options.runtimeConfig.public.nuxtUiMongocampOptions = defu(
36
+ nuxt.options.runtimeConfig.public.nuxtUiMongocampOptions,
37
+ options
38
+ );
27
39
  const { resolve } = createResolver(import.meta.url);
28
40
  const runtimeDir = resolve("./runtime");
29
41
  addPlugin(resolve(runtimeDir, "plugin"));
@@ -0,0 +1,7 @@
1
+ export declare function useMongocampRoles(): {
2
+ isLoggedIn: import("vue").ComputedRef<boolean>;
3
+ isAdmin: import("vue").ComputedRef<boolean>;
4
+ isManager: import("vue").ComputedRef<boolean>;
5
+ notAllowedPath: string;
6
+ isAllowedPathForRoute: (route: string) => boolean;
7
+ };
@@ -0,0 +1,37 @@
1
+ import { useRuntimeConfig } from "#app";
2
+ import { minimatch } from "minimatch";
3
+ import { useMongocampAuth, useMongocampStorage } from "#imports";
4
+ import { computed } from "vue";
5
+ export function useMongocampRoles() {
6
+ const config = useRuntimeConfig();
7
+ const { isLoggedIn } = useMongocampAuth();
8
+ const state = useMongocampStorage();
9
+ const notAllowedPath = config.public.nuxtUiMongocampOptions.notAllowedPath ?? "/";
10
+ const managerRoles = config.public.nuxtUiMongocampOptions.managerRoles ?? [];
11
+ const securedRouteParts = config.public.nuxtUiMongocampOptions.securedRouteParts ?? [];
12
+ const managementRouteParts = config.public.nuxtUiMongocampOptions.managementRouteParts ?? [];
13
+ const adminRouteParts = config.public.nuxtUiMongocampOptions.adminRouteParts ?? [];
14
+ const isAdmin = computed(() => state.value?.profile?.isAdmin ?? false);
15
+ const roles = computed(() => state.value?.profile?.roles ?? []);
16
+ function matchesAnyRole(allowedRoles) {
17
+ return allowedRoles.some((role) => roles.value.includes(role));
18
+ }
19
+ const isManager = computed(() => isAdmin.value || matchesAnyRole(managerRoles));
20
+ function matchesAnyPattern(route, patterns) {
21
+ return patterns.some((pattern) => minimatch(route, pattern) || minimatch(`${route}/`, pattern));
22
+ }
23
+ function isAllowedPathForRoute(route) {
24
+ if (route === notAllowedPath) {
25
+ return true;
26
+ }
27
+ if (!isLoggedIn.value && matchesAnyPattern(route, securedRouteParts)) {
28
+ return false;
29
+ } else if (!isManager.value && matchesAnyPattern(route, managementRouteParts)) {
30
+ return false;
31
+ } else if (!isAdmin.value && matchesAnyPattern(route, adminRouteParts)) {
32
+ return false;
33
+ }
34
+ return true;
35
+ }
36
+ return { isLoggedIn, isAdmin, isManager, notAllowedPath, isAllowedPathForRoute };
37
+ }
@@ -1,20 +1,24 @@
1
- import { defineNuxtPlugin, addRouteMiddleware, navigateTo, useAsyncData } from "#app";
2
- import { useMongocampStorage, useMongocampApi, useMongocampAuth } from "#imports";
1
+ import { defineNuxtPlugin, addRouteMiddleware, navigateTo, useAsyncData, useRuntimeConfig } from "#app";
2
+ import { useMongocampApi, useMongocampAuth } from "#imports";
3
+ import { useMongocampRoles } from "./composables/useMongocampRoles.js";
3
4
  export default defineNuxtPlugin(async (_nuxtApp) => {
4
- const { logout, isLoggedIn } = useMongocampAuth();
5
+ const { logout } = useMongocampAuth();
5
6
  const { informationApi } = useMongocampApi();
6
- const state = useMongocampStorage();
7
+ const { notAllowedPath, isAllowedPathForRoute } = useMongocampRoles();
8
+ const config = useRuntimeConfig();
9
+ const useGlobalAuthMiddleware = config.public.nuxtUiMongocampOptions.useGlobalAuthMiddleware ?? false;
7
10
  const { data: version } = await useAsyncData("version", () => informationApi.version());
8
- addRouteMiddleware("global-auth", (to) => {
9
- if ((to.path.startsWith("/admin") || to.path.startsWith("/secured/admin")) && (!isLoggedIn.value || !state.value.profile.isAdmin)) {
10
- return navigateTo("/secured");
11
- } else if (to.path.startsWith("/secured") && !isLoggedIn.value) {
12
- return navigateTo("/");
13
- } else if (to.path === "/logout") {
14
- logout();
15
- return navigateTo("/");
16
- }
17
- }, { global: true });
11
+ if (useGlobalAuthMiddleware) {
12
+ addRouteMiddleware("global-auth", (to) => {
13
+ if (to.path === "/logout") {
14
+ logout();
15
+ return navigateTo(notAllowedPath);
16
+ }
17
+ if (!isAllowedPathForRoute(to.path)) {
18
+ return navigateTo(notAllowedPath);
19
+ }
20
+ }, { global: true });
21
+ }
18
22
  return {
19
23
  provide: {
20
24
  mongocampVersion: version
package/dist/types.d.mts CHANGED
@@ -1,3 +1,9 @@
1
+ import type { ModulePublicRuntimeConfig } from './module.mjs'
2
+
3
+ declare module '@nuxt/schema' {
4
+ interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
5
+ }
6
+
1
7
  export { default } from './module.mjs'
2
8
 
3
- export { type ModuleOptions } from './module.mjs'
9
+ export { type ModuleOptions, type ModulePublicRuntimeConfig } from './module.mjs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sfxcode/nuxt-ui-mongocamp",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "A Nuxt module that wraps @sfxcode/nuxt-mongocamp-server with ready-made UI components, composables, and a runtime plugin",
5
5
  "repository": "sfxcode/nuxt-ui-mongocamp",
6
6
  "homepage": "https://sfxcode.github.io/nuxt-ui-mongocamp/",
@@ -59,6 +59,8 @@
59
59
  "@nuxt/ui": "4.9.0",
60
60
  "@sfxcode/nuxt-mongocamp-server": "^1.4.4",
61
61
  "@sfxcode/nuxt-ui-formkit": "^1.2.6",
62
+ "defu": "^6.1.7",
63
+ "minimatch": "^10.2.5",
62
64
  "unocss-nuxt-ui": "^1.2.2"
63
65
  },
64
66
  "devDependencies": {