feat: add admin route protection via router beforeEach
This commit is contained in:
@@ -9,6 +9,17 @@
|
|||||||
import { createRouter, createWebHistory } from 'vue-router/auto'
|
import { createRouter, createWebHistory } from 'vue-router/auto'
|
||||||
import { setupLayouts } from 'virtual:generated-layouts'
|
import { setupLayouts } from 'virtual:generated-layouts'
|
||||||
import { routes } from 'vue-router/auto-routes'
|
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({
|
const router = createRouter({
|
||||||
history: createWebHistory(import.meta.env.BASE_URL),
|
history: createWebHistory(import.meta.env.BASE_URL),
|
||||||
@@ -18,9 +29,14 @@ const router = createRouter({
|
|||||||
router.beforeEach((to, from, next) => {
|
router.beforeEach((to, from, next) => {
|
||||||
const isAuthenticated = !!localStorage.getItem('access_token')
|
const isAuthenticated = !!localStorage.getItem('access_token')
|
||||||
const requiresAuth = to.meta.requiresAuth === true
|
const requiresAuth = to.meta.requiresAuth === true
|
||||||
|
const requiresAdmin = to.meta.requiresAdmin === true || ADMIN_ROUTES.includes(to.path)
|
||||||
|
|
||||||
|
const authStore = useAuthStore()
|
||||||
|
|
||||||
if (requiresAuth && !isAuthenticated) {
|
if (requiresAuth && !isAuthenticated) {
|
||||||
next({ path: '/autenticarse', replace: true })
|
next({ path: '/autenticarse', replace: true })
|
||||||
|
} else if (requiresAdmin && !authStore.isAdmin) {
|
||||||
|
next({ path: '/', replace: true })
|
||||||
} else {
|
} else {
|
||||||
next()
|
next()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user