Move admin auth guard to Vue Router
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 24s

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-13 11:09:12 +01:00
parent fc9d3c97bf
commit 869d9a168e
2 changed files with 19 additions and 20 deletions

View File

@@ -2,6 +2,8 @@ import { createRouter, createWebHistory } from "vue-router";
import DefaultLayout from "@/layouts/DefaultLayout.vue";
import CVLayout from "@/layouts/CVLayout.vue";
import Landing from "@/views/Landing.vue";
import { useHomeDataStore } from "@/stores/homeData";
import { useAuthStore } from "@/stores/auth";
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
@@ -80,10 +82,24 @@ const router = createRouter({
path: "jobs",
name: "job-applications",
component: () => import("@/views/CV/JobApplications.vue"),
meta: { requiresAdmin: true },
},
],
},
],
});
router.beforeEach(async (to) => {
if (!to.meta.requiresAdmin) return;
const homeData = useHomeDataStore();
if (!homeData.loaded) {
await new Promise((resolve) => {
const stop = homeData.$watch("loaded", (val) => {
if (val) { stop(); resolve(); }
});
});
}
if (!useAuthStore().user.admin) return "/admin";
});
export default router;

View File

@@ -1,14 +1,8 @@
<script setup>
import { ref, watch } from "vue";
import { useRouter, RouterLink } from "vue-router";
import { useAuthStore } from "@/stores/auth";
import { useHomeDataStore } from "@/stores/homeData";
import { ref, onMounted } from "vue";
import { RouterLink } from "vue-router";
import { gql } from "@/graphql";
const auth = useAuthStore();
const homeData = useHomeDataStore();
const router = useRouter();
const applications = ref([]);
const editingId = ref(null);
const editForm = ref({});
@@ -150,18 +144,7 @@ function statusClass(status) {
return map[status] ?? "";
}
watch(
() => homeData.loaded,
(loaded) => {
if (!loaded) return;
if (!auth.user.admin) {
router.push("/admin");
} else {
fetchApplications();
}
},
{ immediate: true },
);
onMounted(fetchApplications);
</script>
<template>