import { createRouter, createWebHistory } from 'vue-router' import { useAuthStore } from '@/stores/auth' const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: [ { path: '/', name: 'map', component: () => import('@/views/map/MapView.vue'), }, { path: '/login', name: 'login', component: () => import('@/views/auth/LoginView.vue'), }, { path: '/register', name: 'register', component: () => import('@/views/auth/RegisterView.vue'), }, { path: '/upload', name: 'upload', component: () => import('@/views/upload/UploadView.vue'), meta: { requiresAuth: true }, }, { path: '/encyclopedia', name: 'encyclopedia', component: () => import('@/views/encyclopedia/EncyclopediaView.vue'), }, { path: '/encyclopedia/:id', name: 'cloud-type', component: () => import('@/views/encyclopedia/CloudTypeView.vue'), }, { path: '/gallery', name: 'gallery', component: () => import('@/views/gallery/GalleryView.vue'), }, { path: '/profile', name: 'profile', component: () => import('@/views/profile/ProfileView.vue'), meta: { requiresAuth: true }, }, { path: '/profile/:id', name: 'user-profile', component: () => import('@/views/profile/ProfileView.vue'), }, { path: '/admin', name: 'admin', component: () => import('@/views/admin/AdminView.vue'), meta: { requiresAuth: true, requiresAdmin: true }, }, ], scrollBehavior() { return { top: 0 } }, }) router.beforeEach((to, _from, next) => { const authStore = useAuthStore() if (to.meta.requiresAuth && !authStore.isLoggedIn) { next({ name: 'login', query: { redirect: to.fullPath } }) } else if (to.meta.requiresAdmin && !authStore.isAdmin) { next({ name: 'map' }) } else if ((to.name === 'login' || to.name === 'register') && authStore.isLoggedIn) { next({ name: 'map' }) } else { next() } }) export default router