Skip to content

requireGroups ​

Overview ​

Require the user to have certain groups, or show a toast message for denial.

Signature ​

requireGroups(instance, toastArgs, groups, redirectTo, to, router, pinia)

Parameters ​

NameTypeRequiredDescription
instanceAppyesThe Vue app instance.
toastArgsobjectyesToast message options.
groupsstring[]yesThe groups the user must belong to at least one.
redirectTostring | RouteLocationAsRelativeGeneric | RouteLocationAsPathGenericyesWhere to redirect if not authorized.
toRouteLocationNormalizedLoadedGenericyesThe target route.
routerRouteryesThe router instance.
piniaPiniayesThe Pinia instance.

toastArgs Properties ​

NameTypeRequiredDefaultDescription
detailstringno
lifenumberno
severitystringno
summarystring

Returns ​

Promise

True if authorized, or redirect route. Rejects with the request error when the current-user request fails, which rejects the navigation.

Examples ​

js
import { requireGroups } from "@vueda/router/guards";
import { createRouter, createWebHistory } from "vue-router";

const toastArgs = {
  summary: "Permission Denied",
  detail: "You do not have permission to access",
  severity: "error",
};

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/groups-required/',
      name: 'groups-required',
      component: () => import('@/views/ViewGroupsRequired.vue'),
      beforeEnter: (to, from) =>
        requireGroups(app, toastArgs, ["group1", "group2"], { name: "access-denied" }, to, router),
    },
    // other routes
  ],
});

export default router;

Source ​

client/lib/router/guards.js:287