/** * router/index.ts * * Automatic routes for `./src/pages/*.vue` */ // Composables import { createRouter, createWebHistory } from 'vue-router/auto' import { setupLayouts } from 'virtual:generated-layouts' import { routes } from 'vue-router/auto-routes' import { useAuthStore } from '@/stores/auth' const ADMIN_ROUTES = [ '/sincronizar_clientes_tryton', '/sincronizar_ventas_tryton', '/sincronizar_productos_tryton', '/ventas_para_tryton', '/cuadres_de_tarro', '/compra_admin', '/cuadrar_tarro', ] const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes: setupLayouts(routes), }) router.beforeEach((to, from, next) => { const isAuthenticated = !!localStorage.getItem('access_token') const requiresAuth = to.meta.requiresAuth === true const requiresAdmin = to.meta.requiresAdmin === true || ADMIN_ROUTES.includes(to.path) const authStore = useAuthStore() if (requiresAuth && !isAuthenticated) { next({ path: '/autenticarse', replace: true }) } else if (requiresAdmin && !authStore.isAdmin) { next({ path: '/', replace: true }) } else { next() } }) // Workaround for https://github.com/vitejs/vite/issues/11804 router.onError((err, to) => { if (err?.message?.includes?.('Failed to fetch dynamically imported module')) { if (!localStorage.getItem('vuetify:dynamic-reload')) { console.log('Reloading page to fix dynamic import error') localStorage.setItem('vuetify:dynamic-reload', 'true') location.assign(to.fullPath) } else { console.error('Dynamic import error, reloading page did not fix it', err) } } else { console.error(err) } }) router.isReady().then(() => { localStorage.removeItem('vuetify:dynamic-reload') }) export default router