import { createRouter, createWebHistory } from "vue-router";
import { getToken } from "./api.js";
import LoginView from "./views/LoginView.vue";
import DashboardView from "./views/DashboardView.vue";
import DeniedView from "./views/DeniedView.vue";
// Гейт ролей: /dashboard и далее — только system_role admin и выше.
// Проверка роли происходит на сервере (require_admin), фронт лишь заранее
// разводит потоки 401 (нет токена -> login) и 403 (роль ниже admin -> denied).
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: "/", redirect: "/dashboard" },
{ path: "/login", component: LoginView },
{ path: "/denied", component: DeniedView },
{
path: "/dashboard",
component: DashboardView,
meta: { requiresAuth: true },
},
],
});
router.beforeEach((to) => {
if (to.meta.requiresAuth && !getToken()) {
return { path: "/login", query: { next: to.fullPath } };
}
});
export { router };