Agregando proteccion a rutas #38
@@ -10,10 +10,5 @@
|
||||
const authStore = useAuthStore();
|
||||
return { authStore };
|
||||
},
|
||||
mounted() {
|
||||
if (!this.authStore.isAdmin) {
|
||||
this.$router.push('/');
|
||||
}
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -3,5 +3,9 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
//
|
||||
definePage({
|
||||
meta: {
|
||||
requiresAuth: true
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -10,10 +10,5 @@
|
||||
const authStore = useAuthStore();
|
||||
return { authStore };
|
||||
},
|
||||
mounted() {
|
||||
if (!this.authStore.isAdmin) {
|
||||
this.$router.push('/');
|
||||
}
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -10,10 +10,5 @@
|
||||
const authStore = useAuthStore();
|
||||
return { authStore };
|
||||
},
|
||||
mounted() {
|
||||
if (!this.authStore.isAdmin) {
|
||||
this.$router.push('/');
|
||||
}
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -141,11 +141,6 @@
|
||||
result: null,
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
if (!this.authStore.isAdmin) {
|
||||
this.$router.push('/');
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
formatItems(ids) {
|
||||
if (!ids || ids.length === 0) return [];
|
||||
|
||||
@@ -128,11 +128,6 @@
|
||||
result: null,
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
if (!this.authStore.isAdmin) {
|
||||
this.$router.push('/');
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
formatItems(ids) {
|
||||
if (!ids || ids.length === 0) return [];
|
||||
|
||||
@@ -98,11 +98,6 @@
|
||||
result: null,
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
if (!this.authStore.isAdmin) {
|
||||
this.$router.push('/');
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
formatItems(ids) {
|
||||
if (!ids || ids.length === 0) return [];
|
||||
|
||||
@@ -3,5 +3,9 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
//
|
||||
definePage({
|
||||
meta: {
|
||||
requiresAuth: true
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -9,10 +9,5 @@
|
||||
const authStore = useAuthStore();
|
||||
return { authStore };
|
||||
},
|
||||
mounted() {
|
||||
if (!this.authStore.isAdmin) {
|
||||
this.$router.push('/');
|
||||
}
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -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')) {
|
||||
|
||||
@@ -39,6 +39,9 @@ export default defineConfig({
|
||||
imports: [
|
||||
'vue',
|
||||
'vue-router',
|
||||
{
|
||||
'unplugin-vue-router': ['definePage'],
|
||||
},
|
||||
],
|
||||
eslintrc: {
|
||||
enabled: true,
|
||||
|
||||
Reference in New Issue
Block a user