diff --git a/src/router/index.js b/src/router/index.js index 9169544..e6f54a5 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -9,6 +9,17 @@ 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), @@ -18,9 +29,14 @@ const router = createRouter({ 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() }