all repos — stealth-developers @ cef0482d713ef8c29f2e070670c40b8efe9c44fc

pkgs/frontend/src/router/index.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
import { createRouter, createWebHistory } from "vue-router";
import HomeView from "@/views/HomeView.vue";
import { useAuthStore } from "@/stores/auth";

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: "/",
			name: "Home",
			component: HomeView,
		},
		{
			path: "/tickets",
			name: "Tickets",
			component: () => import("../views/TicketsView.vue"),
			meta: {
				requiresAuth: true,
			},
		},
		{
			path: "/tickets/:id",
			name: "Ticket Details",
			component: () => import("../views/TicketDetailView.vue"),
			meta: {
				requiresAuth: true,
			},
		},
		{
			path: "/login",
			name: "Login",
			component: () => import("../views/LoginView.vue"),
		},
	],
});

router.beforeEach(async (to, _from, next) => {
	const authStore = useAuthStore();
	await authStore.waitForAuth();
	const isAuthenticated = authStore.isAuthenticated;

	if (to.meta.requiresAuth && !isAuthenticated) {
		next({ name: "Login" });
	} else {
		next();
	}
});

export default router;