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
| Name | Type | Required | Description |
|---|---|---|---|
| instance | App | yes | The Vue app instance. |
| toastArgs | object | yes | Toast message options. |
| groups | string[] | yes | The groups the user must belong to at least one. |
| redirectTo | string | RouteLocationAsRelativeGeneric | RouteLocationAsPathGeneric | yes | Where to redirect if not authorized. |
| to | RouteLocationNormalizedLoadedGeneric | yes | The target route. |
| router | Router | yes | The router instance. |
| pinia | Pinia | yes | The Pinia instance. |
toastArgs Properties
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| detail | string | no | ||
| life | number | no | ||
| severity | string | no | ||
| summary | string |
Returns
Promise
True if authorized, or redirect route.
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:261