import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router'; import { useAuthStore } from '@/stores/auth'; import { loadingBar } from '@/utils/feedback'; import MainLayout from '@/layouts/MainLayout.vue'; export const routes: RouteRecordRaw[] = [ { path: '/login', name: 'login', component: () => import('@/views/Login.vue'), meta: { public: true }, }, { path: '/', component: MainLayout, redirect: '/dashboard', children: [ { path: 'dashboard', name: 'dashboard', component: () => import('@/views/Dashboard.vue') }, { path: 'templates', name: 'templates', component: () => import('@/views/Templates.vue') }, { path: 'artworks', name: 'artworks', component: () => import('@/views/Artworks.vue') }, { path: 'artworks/:id', name: 'artwork-detail', component: () => import('@/views/ArtworkEdit.vue') }, { path: 'trace-codes', name: 'traceCodes', component: () => import('@/views/TraceCodes.vue') }, { path: 'outbound', name: 'outbound', component: () => import('@/views/Outbound.vue') }, { path: 'query-logs', name: 'queryLogs', component: () => import('@/views/QueryLogs.vue') }, { path: 'users', name: 'users', component: () => import('@/views/Users.vue') }, { path: 'settings', name: 'settings', component: () => import('@/views/Settings.vue') }, { path: 'profile', name: 'profile', component: () => import('@/views/Profile.vue') }, ], }, { path: '/:pathMatch(.*)*', redirect: '/dashboard' }, ]; const router = createRouter({ history: createWebHashHistory(), routes, }); router.beforeEach(async (to) => { loadingBar.start(); const auth = useAuthStore(); if (to.meta.public) { if (auth.isLoggedIn && to.name === 'login') return { name: 'dashboard' }; return true; } if (!auth.isLoggedIn) { return { name: 'login' }; } if (!auth.user) { try { await auth.fetchProfile(); } catch { auth.logout(); return { name: 'login' }; } } return true; }); router.afterEach(() => loadingBar.finish()); export default router;