Agregando proteccion a rutas #38

Merged
mono merged 3 commits from add-auth-protection-routes into main 2026-03-15 23:54:32 -05:00
11 changed files with 40 additions and 37 deletions

View File

@@ -10,10 +10,5 @@
const authStore = useAuthStore();
return { authStore };
},
mounted() {
if (!this.authStore.isAdmin) {
this.$router.push('/');
}
},
}
</script>

View File

@@ -3,5 +3,9 @@
</template>
<script setup>
//
definePage({
meta: {
requiresAuth: true
}
})
</script>

View File

@@ -10,10 +10,5 @@
const authStore = useAuthStore();
return { authStore };
},
mounted() {
if (!this.authStore.isAdmin) {
this.$router.push('/');
}
},
}
</script>

View File

@@ -10,10 +10,5 @@
const authStore = useAuthStore();
return { authStore };
},
mounted() {
if (!this.authStore.isAdmin) {
this.$router.push('/');
}
},
}
</script>

View File

@@ -141,11 +141,6 @@
result: null,
}
},
mounted() {
if (!this.authStore.isAdmin) {
this.$router.push('/');
}
},
methods: {
formatItems(ids) {
if (!ids || ids.length === 0) return [];

View File

@@ -128,11 +128,6 @@
result: null,
}
},
mounted() {
if (!this.authStore.isAdmin) {
this.$router.push('/');
}
},
methods: {
formatItems(ids) {
if (!ids || ids.length === 0) return [];

View File

@@ -98,11 +98,6 @@
result: null,
}
},
mounted() {
if (!this.authStore.isAdmin) {
this.$router.push('/');
}
},
methods: {
formatItems(ids) {
if (!ids || ids.length === 0) return [];

View File

@@ -3,5 +3,9 @@
</template>
<script setup>
//
definePage({
meta: {
requiresAuth: true
}
})
</script>

View File

@@ -9,10 +9,5 @@
const authStore = useAuthStore();
return { authStore };
},
mounted() {
if (!this.authStore.isAdmin) {
this.$router.push('/');
}
},
}
</script>

View File

@@ -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')) {

View File

@@ -39,6 +39,9 @@ export default defineConfig({
imports: [
'vue',
'vue-router',
{
'unplugin-vue-router': ['definePage'],
},
],
eslintrc: {
enabled: true,