import { createWebHistory, createRouter } from "vue-router"; import store from "./AppStore"; // 공통페이지 import Login from "./login/Login.vue"; import MyInfo from "./user/MyInfo.vue"; import Main from "./main/Main.vue"; import NotFound from "./etc/NotFound.vue"; const routes = [ { path: "/", name: "MainPage", component: Main }, { path: "/Login.page", name: "Login", component: Login }, { path: "/MyInfo.page", name: "MyInfo", component: MyInfo }, { path: "/notFound.page", name: "NotFoundPage", component: NotFound }, ]; const AppRouter = createRouter({ history: createWebHistory(), routes, // 모든 라우트 이동 후 페이지 상단으로 스크롤 scrollBehavior() { return { top: 0 } }, }); AppRouter.beforeEach((to, from, next) => { const routeExists = AppRouter.getRoutes().some(route => route.path === to.path || (route.name && route.name === to.name)); const authorState = store.state.roles; const { authorization } = to.meta; // 토큰이 없을 경우 if (authorState == null && to.path != '/login.page') { alert('로그인이 필요합니다.'); return next('/login.page'); } // 권한이 없을 경우 if(authorization){ if(!authorization.includes(authorState[0].authority)){ alert('접근 권한이 없습니다.'); return next(from.path); } } if (!routeExists) { next({ name: 'NotFoundPage' }); return; } next(); }); export default AppRouter;