@cryptotaxi247 / CoPilot / commits / ba6bbd29

updated route check

Davide Di Modica committed Oct 12, 2023 at 18:29 UTC ba6bbd29d051acd14b70082d756c8fbd0e52700d
3 files changed +30 -89
src/middleware/auth.global.ts deleted
-35
@@ -1,35 +0,0 @@
1 -import { useAuthStore } from "@/stores/auth"
2 -import { type RouteMetaAuth } from "@/types/auth.d"
3 -import { type RouteLocationNormalized } from "vue-router"
4 -
5 -export function authCheck(route: RouteLocationNormalized) {
6 - const meta: RouteMetaAuth = route.meta
7 - const { checkAuth, authRedirect, auth, roles } = meta
8 -
9 - if (route?.redirectedFrom?.name === "logout") {
10 - useAuthStore().setLogout()
11 - }
12 -
13 - if (checkAuth === true) {
14 - if (useAuthStore().isLogged) {
15 - if (roles) {
16 - if (useAuthStore().isRoleGranted(roles)) {
17 - return authRedirect || "/"
18 - } else {
19 - return route.path
20 - }
21 - }
22 - return authRedirect || "/"
23 - }
24 - }
25 -
26 - if (auth === true) {
27 - if (!useAuthStore().isLogged) {
28 - return "/login"
29 - }
30 -
31 - if (roles && !useAuthStore().isRoleGranted(roles)) {
32 - return "/login"
33 - }
34 - }
35 -}
src/router/components.ts
+3 -1
@@ -1,9 +1,11 @@
1 +import { UserRole } from "@/types/auth.d"
2 +
3 export default {
4 path: "/components",
5 redirect: "/components/avatar",
6 meta: {
7 auth: true,
6 - roles: "all"
8 + roles: UserRole.All
9 },
10 children: [
11 {
src/router/index.ts
+27 -53
@@ -1,5 +1,7 @@
1 +import { UserRole } from "@/types/auth.d"
2 import { createRouter, createWebHistory } from "vue-router"
3 import Analytics from "@/views/Dashboard/Analytics.vue"
4 +import { authCheck } from "@/utils/auth"
5 import components from "./components"
6 import { Layout } from "@/types/theme.d"
7
@@ -14,33 +16,25 @@ const router = createRouter({
16 path: "/indices",
17 name: "indices",
18 component: () => import("@/views/socfortress/Indices.vue"),
17 - meta: { title: "Indices", auth: true, roles: "all" }
19 + meta: { title: "Indices", auth: true, roles: UserRole.All }
20 },
19 - /*
20 - {
21 - path: "/inputs",
22 - name: "inputs",
23 - component: () => import("@/views/socfortress/Inputs.vue"),
24 - meta: { title: "Inputs", auth: true, roles: "all" }
25 - },
26 - */
21 {
22 path: "/connectors",
23 name: "connectors",
24 component: () => import("@/views/socfortress/Connectors.vue"),
31 - meta: { title: "Connectors", auth: true, roles: "all" }
25 + meta: { title: "Connectors", auth: true, roles: UserRole.All }
26 },
27 {
28 path: "/agents",
29 name: "agents",
30 component: () => import("@/views/socfortress/Agents.vue"),
37 - meta: { title: "Agents", auth: true, roles: "all" }
31 + meta: { title: "Agents", auth: true, roles: UserRole.All }
32 },
33 {
34 path: "/agent/:id?",
35 name: "agent",
36 component: () => import("@/views/socfortress/AgentOverview.vue"),
43 - meta: { title: "Agent", auth: true, roles: "all" }
37 + meta: { title: "Agent", auth: true, roles: UserRole.All }
38 },
39
40 {
@@ -48,7 +42,7 @@ const router = createRouter({
42 redirect: "/dashboard/analytics",
43 meta: {
44 auth: true,
51 - roles: "all"
45 + roles: UserRole.All
46 },
47 children: [
48 {
@@ -69,68 +63,44 @@ const router = createRouter({
63 path: "/calendar",
64 name: "calendar",
65 component: () => import("@/views/Apps/Calendars/FullCalendar.vue"),
72 - meta: { title: "Calendar", auth: true, roles: "all" }
66 + meta: { title: "Calendar", auth: true, roles: UserRole.All }
67 },
74 - /*
75 - {
76 - path: "/calendars",
77 - redirect: "/calendars/vue-cal",
78 - meta: {
79 - auth: true,
80 - roles: "all"
81 - },
82 - children: [
83 - {
84 - path: "vue-cal",
85 - name: "vue-cal",
86 - component: () => import("@/views/Apps/Calendars/VueCal.vue"),
87 - meta: { title: "Vue Cal" }
88 - },
89 - {
90 - path: "full-calendar",
91 - name: "full-calendar",
92 - component: () => import("@/views/Apps/Calendars/FullCalendar.vue"),
93 - meta: { title: "Full Calendar" }
94 - }
95 - ]
96 - },
97 - */
68 {
69 path: "/email",
70 name: "email",
71 component: () => import("@/views/Apps/Mailbox.vue"),
102 - meta: { title: "Email", auth: true, roles: "all" }
72 + meta: { title: "Email", auth: true, roles: UserRole.All }
73 },
74 {
75 path: "/chat",
76 name: "chat",
77 component: () => import("@/views/Apps/Chat.vue"),
108 - meta: { title: "Chat", auth: true, roles: "all" }
78 + meta: { title: "Chat", auth: true, roles: UserRole.All }
79 },
80 {
81 path: "/kanban",
82 name: "kanban",
83 component: () => import("@/views/Apps/Kanban.vue"),
114 - meta: { title: "Kanban", auth: true, roles: "all" }
84 + meta: { title: "Kanban", auth: true, roles: UserRole.All }
85 },
86 {
87 path: "/notes",
88 name: "notes",
89 component: () => import("@/views/Apps/Notes.vue"),
120 - meta: { title: "Notes", auth: true, roles: "all" }
90 + meta: { title: "Notes", auth: true, roles: UserRole.All }
91 },
92 {
93 path: "/typography",
94 name: "typography",
95 component: () => import("@/views/Typography.vue"),
126 - meta: { title: "Typography", auth: true, roles: "all" }
96 + meta: { title: "Typography", auth: true, roles: UserRole.All }
97 },
98 {
99 path: "/cards",
100 redirect: "/cards/basic",
101 meta: {
102 auth: true,
133 - roles: "all"
103 + roles: UserRole.All
104 },
105 children: [
106 {
@@ -171,7 +141,7 @@ const router = createRouter({
141 redirect: "/toolbox/refresh-tool",
142 meta: {
143 auth: true,
174 - roles: "all"
144 + roles: UserRole.All
145 },
146 children: [
147 {
@@ -196,7 +166,7 @@ const router = createRouter({
166 redirect: "/layout/left-sidebar",
167 meta: {
168 auth: true,
199 - roles: "all"
169 + roles: UserRole.All
170 },
171 children: [
172 {
@@ -224,7 +194,7 @@ const router = createRouter({
194 redirect: "/maps/google-maps",
195 meta: {
196 auth: true,
227 - roles: "all"
197 + roles: UserRole.All
198 },
199 children: [
200 {
@@ -258,7 +228,7 @@ const router = createRouter({
228 redirect: "/editors/quill",
229 meta: {
230 auth: true,
261 - roles: "all"
231 + roles: UserRole.All
232 },
233 children: [
234 {
@@ -286,7 +256,7 @@ const router = createRouter({
256 redirect: "/charts/apexcharts",
257 meta: {
258 auth: true,
289 - roles: "all"
259 + roles: UserRole.All
260 },
261 children: [
262 {
@@ -307,14 +277,14 @@ const router = createRouter({
277 path: "/multi-language",
278 name: "multi-language",
279 component: () => import("@/views/MultiLanguage.vue"),
310 - meta: { title: "Multi Language", auth: true, roles: "all" }
280 + meta: { title: "Multi Language", auth: true, roles: UserRole.All }
281 },
282 {
283 path: "/icons",
284 redirect: "/icons/xicons",
285 meta: {
286 auth: true,
317 - roles: "all"
287 + roles: UserRole.All
288 },
289 children: [
290 {
@@ -336,7 +306,7 @@ const router = createRouter({
306 redirect: "/tables/base",
307 meta: {
308 auth: true,
339 - roles: "all"
309 + roles: UserRole.All
310 },
311 children: [
312 {
@@ -358,7 +328,7 @@ const router = createRouter({
328 path: "/profile",
329 name: "profile",
330 component: () => import("@/views/Profile.vue"),
361 - meta: { title: "Profile", auth: true, roles: "all" }
331 + meta: { title: "Profile", auth: true, roles: UserRole.All }
332 },
333
334 {
@@ -381,4 +351,8 @@ const router = createRouter({
351 ]
352 })
353
354 +router.beforeEach(route => {
355 + return authCheck(route)
356 +})
357 +
358 export default router