Move admin auth guard to Vue Router
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 24s
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:
@@ -2,6 +2,8 @@ import { createRouter, createWebHistory } from "vue-router";
|
|||||||
import DefaultLayout from "@/layouts/DefaultLayout.vue";
|
import DefaultLayout from "@/layouts/DefaultLayout.vue";
|
||||||
import CVLayout from "@/layouts/CVLayout.vue";
|
import CVLayout from "@/layouts/CVLayout.vue";
|
||||||
import Landing from "@/views/Landing.vue";
|
import Landing from "@/views/Landing.vue";
|
||||||
|
import { useHomeDataStore } from "@/stores/homeData";
|
||||||
|
import { useAuthStore } from "@/stores/auth";
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(import.meta.env.BASE_URL),
|
history: createWebHistory(import.meta.env.BASE_URL),
|
||||||
@@ -80,10 +82,24 @@ const router = createRouter({
|
|||||||
path: "jobs",
|
path: "jobs",
|
||||||
name: "job-applications",
|
name: "job-applications",
|
||||||
component: () => import("@/views/CV/JobApplications.vue"),
|
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;
|
export default router;
|
||||||
|
|||||||
@@ -1,14 +1,8 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, watch } from "vue";
|
import { ref, onMounted } from "vue";
|
||||||
import { useRouter, RouterLink } from "vue-router";
|
import { RouterLink } from "vue-router";
|
||||||
import { useAuthStore } from "@/stores/auth";
|
|
||||||
import { useHomeDataStore } from "@/stores/homeData";
|
|
||||||
import { gql } from "@/graphql";
|
import { gql } from "@/graphql";
|
||||||
|
|
||||||
const auth = useAuthStore();
|
|
||||||
const homeData = useHomeDataStore();
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
const applications = ref([]);
|
const applications = ref([]);
|
||||||
const editingId = ref(null);
|
const editingId = ref(null);
|
||||||
const editForm = ref({});
|
const editForm = ref({});
|
||||||
@@ -150,18 +144,7 @@ function statusClass(status) {
|
|||||||
return map[status] ?? "";
|
return map[status] ?? "";
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(
|
onMounted(fetchApplications);
|
||||||
() => homeData.loaded,
|
|
||||||
(loaded) => {
|
|
||||||
if (!loaded) return;
|
|
||||||
if (!auth.user.admin) {
|
|
||||||
router.push("/admin");
|
|
||||||
} else {
|
|
||||||
fetchApplications();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ immediate: true },
|
|
||||||
);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
Reference in New Issue
Block a user