From ce0f6683e31cf38d625704a2374665b37067ec7f Mon Sep 17 00:00:00 2001 From: Bram Reyniers Date: Wed, 22 May 2024 16:50:46 +0200 Subject: [PATCH] allow admins to visit all pages --- frontend/src/router/middleware/canVisit.ts | 39 +++++++++++----------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/frontend/src/router/middleware/canVisit.ts b/frontend/src/router/middleware/canVisit.ts index 8ab3bcc6..58fcfecc 100644 --- a/frontend/src/router/middleware/canVisit.ts +++ b/frontend/src/router/middleware/canVisit.ts @@ -1,4 +1,4 @@ -import { computed, inject, type Ref } from "vue"; +import { computed, type Ref } from "vue"; import type { Middleware, MiddlewareContext } from "./index"; import { QueryClient } from "@tanstack/vue-query"; import useIsAdmin from "@/composables/useIsAdmin"; @@ -15,30 +15,31 @@ export interface CanVisitCondition { ): { condition: Ref; isLoading: Ref }; } +function useAwaitLoading(isLoading: Ref): Promise { + return new Promise((resolve) => { + const interval = setInterval(() => { + if (!isLoading.value) { + clearInterval(interval); + resolve(); + } + }, 10); + }); +} + function useCanVisit(useCondition: CanVisitCondition): Middleware { return async (context) => { const { next } = context; - // TODO: Figure out why this doesn't work anymore - // const queryClient = inject("queryClient", new QueryClient()); const queryClient = new QueryClient(); + const { condition: isAdmin, isLoading: isAdminLoading } = useIsAdminCondition(queryClient, context); const { condition, isLoading } = useCondition(queryClient, context); - const awaitLoading = () => - new Promise((resolve) => { - const interval = setInterval(() => { - if (!isLoading.value) { - clearInterval(interval); - resolve(); - } - }, 10); - }); - await awaitLoading(); - if (!condition.value) { - return { - next: () => next({ path: "not-found" }), - final: true, - }; + await useAwaitLoading(computed(() => isAdminLoading.value || isLoading.value)); + if (isAdmin.value || condition.value) { + return { next, final: false }; } - return { next, final: false }; + return { + next: () => next({ path: "not-found" }), + final: true, + }; }; }