Newer
Older
gn-synapse / frontend / src / router.js
import { createRouter, createWebHistory } from "vue-router";
import { clearToken, getToken } from "./api.js";
import LoginView from "./views/LoginView.vue";
import DashboardView from "./views/DashboardView.vue";
import DeniedView from "./views/DeniedView.vue";
import OauthCallbackView from "./views/OauthCallbackView.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: "/oauth/callback", component: OauthCallbackView },
    {
      path: "/dashboard",
      component: DashboardView,
      meta: { requiresAuth: true },
    },
  ],
});

router.beforeEach((to) => {
  if (to.meta.requiresAuth && !getToken()) {
    return { path: "/login", query: { next: to.fullPath } };
  }
});

export { router };