Files
XH-XunKe/frontend/src/router/index.ts
T

30 lines
1.2 KiB
TypeScript

import { createRouter, createWebHistory } from "vue-router";
import { useAuthStore } from "../stores/auth";
export const router = createRouter({
history: createWebHistory(),
routes: [
{ path: "/login", component: () => import("../views/Login.vue") },
{ path: "/forgot", component: () => import("../views/Forgot.vue") },
{ path: "/register", component: () => import("../views/Register.vue") },
{ path: "/", component: () => import("../views/Home.vue") },
{ path: "/studio", component: () => import("../views/Studio.vue"), meta: { auth: true } },
{ path: "/watch/:id", component: () => import("../views/Watch.vue"), meta: { auth: true } },
{ path: "/inspect", component: () => import("../views/Inspect.vue"), meta: { auth: true } },
{ path: "/admin", component: () => import("../views/Admin.vue"), meta: { auth: true } },
{ path: "/replays", component: () => import("../views/Replays.vue"), meta: { auth: true } }
]
});
router.beforeEach(async (to) => {
if (!to.meta.auth) return true;
const auth = useAuthStore();
if (auth.user) return true;
try {
await auth.refresh();
return true;
} catch {
return "/login";
}
});