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"; import TotalSearch from "./user/TotalSearch.vue"; import PicHistorySearch from "./user/PicHistorySearch.vue"; import PicHistoryInsert from "./user/PicHistoryInsert.vue"; const routes = [ { path: "/", name: "MainPage", component: Main, meta: { authorization: ['ROLE_ADMIN', 'ROLE_USER']} }, { path: "/Login.page", name: "Login", component: Login }, { path: "/MyInfo.page", name: "MyInfo", component: MyInfo, meta: { authorization: ['ROLE_ADMIN', 'ROLE_USER']} }, { path: "/notFound.page", name: "NotFoundPage", component: NotFound }, { path: "/TotalSearch.page", name: "TotalSearch", component: TotalSearch }, { path: "/PicHistorySearch.page", name: "PicHistorySearch", component: PicHistorySearch }, { path: "/PicHistoryInsert.page", name: "PicHistoryInsert", component: PicHistoryInsert }, ]; 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 userId = store.state.userId; const { authorization } = to.meta; // 로그인 상태 확인 const isLoggedIn = userId; // 로그인되지 않은 경우 if (isLoggedIn == null && to.path !== '/Login.page') { alert('로그인이 필요합니다.'); return next('/Login.page'); // 로그인 페이지로 리다이렉트 } // 권한이 없을 경우 if(authorization){ if(!authorization.includes(store.state.roles[0].authority)){ alert('접근 권한이 없습니다.'); return next(from.path); } } if (!routeExists) { next({ name: 'NotFoundPage' }); return; } next(); }); export default AppRouter;