diff --git a/src/pages/compra_admin.vue b/src/pages/compra_admin.vue index aef0751..14d1221 100644 --- a/src/pages/compra_admin.vue +++ b/src/pages/compra_admin.vue @@ -10,10 +10,5 @@ const authStore = useAuthStore(); return { authStore }; }, - mounted() { - if (!this.authStore.isAdmin) { - this.$router.push('/'); - } - }, } diff --git a/src/pages/comprar.vue b/src/pages/comprar.vue index 124c4fe..970ce2d 100644 --- a/src/pages/comprar.vue +++ b/src/pages/comprar.vue @@ -3,5 +3,9 @@ diff --git a/src/pages/cuadrar_tarro.vue b/src/pages/cuadrar_tarro.vue index 9035119..dfd0300 100644 --- a/src/pages/cuadrar_tarro.vue +++ b/src/pages/cuadrar_tarro.vue @@ -10,10 +10,5 @@ const authStore = useAuthStore(); return { authStore }; }, - mounted() { - if (!this.authStore.isAdmin) { - this.$router.push('/'); - } - }, } diff --git a/src/pages/cuadres_de_tarro.vue b/src/pages/cuadres_de_tarro.vue index 8678601..9d86b5e 100644 --- a/src/pages/cuadres_de_tarro.vue +++ b/src/pages/cuadres_de_tarro.vue @@ -10,10 +10,5 @@ const authStore = useAuthStore(); return { authStore }; }, - mounted() { - if (!this.authStore.isAdmin) { - this.$router.push('/'); - } - }, } diff --git a/src/pages/sincronizar_clientes_tryton.vue b/src/pages/sincronizar_clientes_tryton.vue index 6fee0e3..c032c74 100644 --- a/src/pages/sincronizar_clientes_tryton.vue +++ b/src/pages/sincronizar_clientes_tryton.vue @@ -141,11 +141,6 @@ result: null, } }, - mounted() { - if (!this.authStore.isAdmin) { - this.$router.push('/'); - } - }, methods: { formatItems(ids) { if (!ids || ids.length === 0) return []; diff --git a/src/pages/sincronizar_productos_tryton.vue b/src/pages/sincronizar_productos_tryton.vue index d091ee5..a0aad2c 100644 --- a/src/pages/sincronizar_productos_tryton.vue +++ b/src/pages/sincronizar_productos_tryton.vue @@ -128,11 +128,6 @@ result: null, } }, - mounted() { - if (!this.authStore.isAdmin) { - this.$router.push('/'); - } - }, methods: { formatItems(ids) { if (!ids || ids.length === 0) return []; diff --git a/src/pages/sincronizar_ventas_tryton.vue b/src/pages/sincronizar_ventas_tryton.vue index 232c291..e413a3e 100644 --- a/src/pages/sincronizar_ventas_tryton.vue +++ b/src/pages/sincronizar_ventas_tryton.vue @@ -98,11 +98,6 @@ result: null, } }, - mounted() { - if (!this.authStore.isAdmin) { - this.$router.push('/'); - } - }, methods: { formatItems(ids) { if (!ids || ids.length === 0) return []; diff --git a/src/pages/summary_purchase.vue b/src/pages/summary_purchase.vue index 2fcd7ff..8ef1288 100644 --- a/src/pages/summary_purchase.vue +++ b/src/pages/summary_purchase.vue @@ -3,5 +3,9 @@ diff --git a/src/pages/ventas_para_tryton.vue b/src/pages/ventas_para_tryton.vue index 3d751a3..0c14bf8 100644 --- a/src/pages/ventas_para_tryton.vue +++ b/src/pages/ventas_para_tryton.vue @@ -9,10 +9,5 @@ const authStore = useAuthStore(); return { authStore }; }, - mounted() { - if (!this.authStore.isAdmin) { - this.$router.push('/'); - } - }, } diff --git a/src/router/index.js b/src/router/index.js index 4e2db62..e6f54a5 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -9,12 +9,39 @@ 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')) { diff --git a/vite.config.mjs b/vite.config.mjs index bfb192d..2369908 100644 --- a/vite.config.mjs +++ b/vite.config.mjs @@ -39,6 +39,9 @@ export default defineConfig({ imports: [ 'vue', 'vue-router', + { + 'unplugin-vue-router': ['definePage'], + }, ], eslintrc: { enabled: true,