This commit is contained in:
SeoJHeasdw
2025-06-12 09:35:44 +09:00
parent a9005e6dc4
commit a5aaff660c
7 changed files with 523 additions and 631 deletions
+22 -58
View File
@@ -4,26 +4,18 @@
* 라우팅 및 네비게이션 가드 설정
*/
import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/store/auth'
// 뷰 컴포넌트 lazy loading
const LoginView = () => import('@/views/LoginView.vue')
const DashboardView = () => import('@/views/DashboardView.vue')
const StoreManagementView = () => import('@/views/StoreManagementView.vue')
const MenuManagementView = () => import('@/views/MenuManagementView.vue')
const ContentCreationView = () => import('@/views/ContentCreationView.vue')
const ContentManagementView = () => import('@/views/ContentManagementView.vue')
const AIRecommendationView = () => import('@/views/AIRecommendationView.vue')
const SalesAnalysisView = () => import('@/views/SalesAnalysisView.vue')
const routes = [
{
path: '/',
redirect: (to) => {
// 인증 상태에 따른 동적 리다이렉트
const authStore = useAuthStore()
return authStore.isAuthenticated ? '/dashboard' : '/login'
},
redirect: '/login', // 항상 로그인 페이지로 먼저 리다이렉트
},
{
path: '/login',
@@ -52,15 +44,6 @@ const routes = [
title: '매장 관리',
},
},
{
path: '/menu',
name: 'MenuManagement',
component: MenuManagementView,
meta: {
requiresAuth: true,
title: '메뉴 관리',
},
},
{
path: '/content/create',
name: 'ContentCreation',
@@ -79,24 +62,6 @@ const routes = [
title: '콘텐츠 관리',
},
},
{
path: '/ai-recommend',
name: 'AIRecommendation',
component: AIRecommendationView,
meta: {
requiresAuth: true,
title: 'AI 추천',
},
},
{
path: '/sales',
name: 'SalesAnalysis',
component: SalesAnalysisView,
meta: {
requiresAuth: true,
title: '매출 분석',
},
},
{
path: '/:pathMatch(.*)*',
redirect: '/login', // 404시 로그인으로 이동
@@ -108,41 +73,40 @@ const router = createRouter({
routes,
})
// 네비게이션 가드
// 네비게이션 가드 - 수정된 버전
router.beforeEach(async (to, from, next) => {
console.log('=== 라우터 가드 실행 ===')
console.log('이동 경로:', `${from.path}${to.path}`)
// Pinia 스토어를 동적으로 가져오기 (순환 참조 방지)
const { useAuthStore } = await import('@/store/auth')
const authStore = useAuthStore()
// 인증 상태 확인
authStore.checkAuthState()
console.log('현재 인증 상태:', authStore.isAuthenticated)
console.log('토큰 존재:', !!authStore.token)
console.log('사용자 정보:', authStore.user)
console.log('사용자 정보:', authStore.user?.nickname)
// 인증이 필요한 페이지인지 확인
if (to.meta.requiresAuth) {
console.log('인증이 필요한 페이지')
const requiresAuth = to.meta.requiresAuth !== false
if (!authStore.isAuthenticated) {
console.log('인증되지 않음, 로그인으로 이동')
next('/login')
} else {
console.log('인증됨, 페이지 이동 허용')
next()
}
if (requiresAuth && !authStore.isAuthenticated) {
console.log('인증 필요 - 로그인 페이지로 이동')
next('/login')
} else if (to.path === '/login' && authStore.isAuthenticated) {
console.log('이미 로그인됨 - 대시보드로 이동')
next('/dashboard')
} else {
console.log('인증이 필요하지 않은 페이지')
// 로그인 페이지에 이미 인증된 사용자가 접근하는 경우
if (to.name === 'Login' && authStore.isAuthenticated) {
console.log('이미 로그인됨, 대시보드로 리다이렉트')
next('/dashboard')
} else {
console.log('페이지 이동 허용')
next()
}
console.log('이동 허용:', to.path)
next()
}
})
console.log('=== 라우터 가드 종료 ===')
router.afterEach((to) => {
// 페이지 타이틀 설정
document.title = to.meta.title ? `${to.meta.title} - AI 마케팅` : 'AI 마케팅'
})
export default router