localhost-front/src/router/index.js
dyhj625 701497a0b0
All checks were successful
LocalNet_front/pipeline/head This commit looks good
d
2025-03-18 14:27:21 +09:00

155 lines
4.7 KiB
JavaScript

import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@s/useAuthStore';
import { useUserInfoStore } from '@s/useUserInfoStore';
// 초기 렌더링 속도를 위해 지연 로딩 사용
const routes = [
{
path: '/',
name: "Home",
component: () => import('@v/MainView.vue'),
meta: { requiresAuth: true }
},
{
path: '/board',
component: () => import('@v/board/TheBoard.vue'),
meta: { requiresAuth: true },
children: [
{
path: '',
name: 'BoardList',
component: () => import('@v/board/BoardList.vue')
},
{
path: 'write',
component: () => import('@v/board/BoardWrite.vue')
},
{
path: ':id',
name: 'BoardDetail',
component: () => import('@v/board/BoardView.vue')
},
{
path: 'edit/:id',
name: 'BoardEdit',
component: () => import('@v/board/BoardEdit.vue')
}
]
},
{
path: '/wordDict',
component: () => import('@v/wordDict/wordDict.vue'),
meta: { requiresAuth: true }
},
{
path: '/login',
name: 'Login',
component: () => import('@v/user/TheLogin.vue'),
meta: { layout: 'NoLayout', requiresGuest: true },
},
{
path: '/register',
name: 'Register',
component: () => import('@v/user/TheRegister.vue'),
meta: { layout: 'NoLayout', requiresGuest: true },
},
{
path: '/pw',
name: 'Password',
component: () => import('@v/user/ThePassword.vue'),
meta: { layout: 'NoLayout', requiresGuest: true },
},
{
path: '/vacation',
component: () => import('@v/vacation/VacationManagement.vue'),
meta: { requiresAuth: true }
},
{
path: '/voteboard',
component: () => import('@v/voteboard/TheVoteBoard.vue'),
meta: { requiresAuth: true },
children: [
{
path: '',
component: () => import('@v/voteboard/voteBoardList.vue')
},
{
path: 'write',
component: () => import('@v/voteboard/voteboardWrite.vue')
},
]
},
{
path: '/projectlist',
component: () => import('@v/projectlist/TheProjectList.vue'),
meta: { requiresAuth: true }
},
{
path: '/commuters',
component: () => import('@v/commuters/TheCommuters.vue'),
meta: { requiresAuth: true }
},
{
path: '/authorization',
component: () => import('@v/admin/TheAuthorization.vue'),
meta: { requiresAuth: true }
},
{ path: "/error/400", name: "Error400", component: () => import('@v/error/Error400.vue'), meta: {layout: 'NoLayout'} },
{ path: "/error/500", name: "Error500", component: () => import('@v/error/Error500.vue'), meta: {layout: 'NoLayout'} },
{
path: "/:anything(.*)",
name: "Error404", component: () => import('@v/error/Error404.vue'), meta: {layout: 'NoLayout'}
},
];
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: routes,
})
router.beforeEach(async (to, from, next) => {
const authStore = useAuthStore();
await authStore.checkAuthStatus(); // 로그인 상태 확인
const allowedUserId = 1; // 특정 ID (변경필요!!)
const userStore = useUserInfoStore();
const userId = userStore.user?.id ?? null;
// 로그인이 필요한 페이지인데 로그인되지 않은 경우 → 로그인 페이지로 이동
if (to.meta.requiresAuth && !authStore.isAuthenticated) {
return next({ name: 'Login', query: { redirect: to.fullPath } });
}
// Authorization 페이지는 ID가 1이 아니면 접근 차단
if (to.path === "/authorization" && userId !== allowedUserId) {
return next("/");
}
// 비로그인 사용자만 접근 가능한 페이지인데 로그인된 경우 → 홈으로 이동
if (to.meta.requiresGuest && authStore.isAuthenticated) {
return next({ name: 'Home' });
}
// 모든 조건을 통과하면 정상적으로 이동
next();
});
import axios from 'axios';
axios.interceptors.response.use(
response => response,
error => {
const status = error.response?.status;
if (status === 400) {
router.push({ name: 'Error400' });
} else if (status === 500) {
router.push({ name: 'Error500' });
}
return Promise.reject(error);
}
);
export default router