Skip to content

Commit d6d7783

Browse files
committed
Frontend security
1 parent ad1ee7d commit d6d7783

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/web/src/modules/administration/router/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,25 @@ const routes = [
99
path: "/administration",
1010
component: () => import("../views/Administration.vue"),
1111
beforeEnter: authGuard,
12+
meta: { requiresAuth: true, requireSystemAdmin: true },
1213
},
1314
{
1415
path: "/administration/users",
1516
component: () => import("../views/UserList.vue"),
1617
beforeEnter: authGuard,
18+
meta: { requiresAuth: true, requireSystemAdmin: true },
1719
},
1820
{
1921
path: "/administration/visitor-centres",
2022
component: () => import("../views/CentreList.vue"),
2123
beforeEnter: authGuard,
24+
meta: { requiresAuth: true, requireSystemAdmin: true },
2225
},
2326
{
2427
path: "/administration/kiosks",
2528
component: () => import("../views/KioskData.vue"),
2629
beforeEnter: authGuard,
30+
meta: { requiresAuth: true, requireSystemAdmin: true },
2731
},
2832
],
2933
},

src/web/src/routes.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
2+
import { authGuard } from "@auth0/auth0-vue";
23
import homeRoutes from "@/modules/home/router";
34
import adminstrationRoutes from "@/modules/administration/router";
45
import authenticationRoutes from "@/modules/authentication/router";
@@ -23,7 +24,43 @@ const routes: Array<RouteRecordRaw> = [
2324
},
2425
];
2526

27+
import { useUserStore } from "@/store/UserStore";
28+
29+
export async function waitForUserToLoad(): Promise<any> {
30+
let u = useUserStore();
31+
await u.initialize();
32+
return u;
33+
}
34+
2635
export const router = createRouter({
2736
history: createWebHistory(),
2837
routes,
2938
});
39+
40+
router.beforeEach(async (to) => {
41+
console.log("BEFORE", to.meta.requiresAuth, to.meta.requireSystemAdmin);
42+
43+
if (to.meta.requiresAuth === false) {
44+
console.log("route allowed - no auth required");
45+
return true;
46+
}
47+
48+
console.log("Await authGuard");
49+
const isAuthenticated = await authGuard(to);
50+
51+
if (isAuthenticated) {
52+
console.log("You are authenticated");
53+
54+
if (to.meta.requireSystemAdmin) {
55+
const u = await waitForUserToLoad();
56+
console.log("User Is Admin", u.isAdmin);
57+
return u.isAdmin;
58+
}
59+
60+
console.log(" route allowed");
61+
return true;
62+
}
63+
64+
console.log("You are NOT authenticated - route blocked");
65+
return false;
66+
});

src/web/src/store/UserStore.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { defineStore } from "pinia";
2-
import { useNotificationStore } from "@/store/NotificationStore";
32
import { useApiStore } from "@/store/ApiStore";
43
import { PERMISSION_URL, PROFILE_URL } from "@/urls";
5-
import { useCentreStore } from "@/modules/centre/store";
64
import { UserScope } from "./models";
75

8-
let m = useNotificationStore();
9-
let c = useCentreStore();
10-
116
export const useUserStore = defineStore("user", {
127
state: () => ({
138
user: {

0 commit comments

Comments
 (0)