From eb4811ab8bbd528cc740c7339ee0b0d38fd3daf8 Mon Sep 17 00:00:00 2001 From: mono Date: Sun, 15 Mar 2026 23:32:36 -0500 Subject: [PATCH 1/3] feat: add auth protection to routes using meta fields --- src/pages/comprar.vue | 6 +++++- src/pages/summary_purchase.vue | 6 +++++- src/router/index.js | 11 +++++++++++ vite.config.mjs | 3 +++ 4 files changed, 24 insertions(+), 2 deletions(-) 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/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/router/index.js b/src/router/index.js index 4e2db62..9169544 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -15,6 +15,17 @@ const router = createRouter({ routes: setupLayouts(routes), }) +router.beforeEach((to, from, next) => { + const isAuthenticated = !!localStorage.getItem('access_token') + const requiresAuth = to.meta.requiresAuth === true + + if (requiresAuth && !isAuthenticated) { + next({ path: '/autenticarse', 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, -- 2.49.1 From c1fccc4d53cb452769cbb99654294e752f58813e Mon Sep 17 00:00:00 2001 From: mono Date: Sun, 15 Mar 2026 23:46:08 -0500 Subject: [PATCH 2/3] feat: add admin route protection via router beforeEach --- src/router/index.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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() } -- 2.49.1 From faa5cf1a4681acdc759b8a55511075769db8bb94 Mon Sep 17 00:00:00 2001 From: mono Date: Sun, 15 Mar 2026 23:54:34 -0500 Subject: [PATCH 3/3] refactor: remove redundant admin checks from pages (now handled by router) --- src/pages/compra_admin.vue | 5 ----- src/pages/cuadrar_tarro.vue | 5 ----- src/pages/cuadres_de_tarro.vue | 5 ----- src/pages/sincronizar_clientes_tryton.vue | 5 ----- src/pages/sincronizar_productos_tryton.vue | 5 ----- src/pages/sincronizar_ventas_tryton.vue | 5 ----- src/pages/ventas_para_tryton.vue | 5 ----- 7 files changed, 35 deletions(-) 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/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/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('/'); - } - }, } -- 2.49.1