You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.5 KiB
JavaScript
73 lines
1.5 KiB
JavaScript
|
2 months ago
|
import { createRouter, createWebHistory } from 'vue-router'
|
||
|
|
import Login from '../views/Login.vue'
|
||
|
|
import Hello from '../components/HelloWorld.vue'
|
||
|
|
import { useUserStore } from '../store/user'
|
||
|
|
|
||
|
|
const routes = [
|
||
|
|
|
||
|
|
{
|
||
|
|
path: '/',
|
||
|
|
component: Hello
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/login',
|
||
|
|
component: Login,
|
||
|
|
meta: { guest: true }
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/usuario/dashboard',
|
||
|
|
name: 'dashboard',
|
||
|
|
component: () => import('../views/usuario/Dashboard.vue'),
|
||
|
|
meta: { requiresAuth: true, role: 'usuario' }
|
||
|
|
},
|
||
|
|
|
||
|
|
{
|
||
|
|
path: '/admin/dashboard',
|
||
|
|
name: 'admin-dashboard',
|
||
|
|
component: () => import('../views/administrador/Dashboard.vue'),
|
||
|
|
meta: { requiresAuth: true, role: 'administrador' }
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/superadmin/dashboard',
|
||
|
|
name: 'superadmin-dashboard',
|
||
|
|
component: () => import('../views/superadmin/Dashboard.vue'),
|
||
|
|
meta: { requiresAuth: true, role: 'superadmin' }
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: '/403',
|
||
|
|
name: 'forbidden',
|
||
|
|
component: () => import('../views/403.vue')
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
const router = createRouter({
|
||
|
|
history: createWebHistory(),
|
||
|
|
routes
|
||
|
|
})
|
||
|
|
|
||
|
|
router.beforeEach((to, from, next) => {
|
||
|
|
const userStore = useUserStore()
|
||
|
|
|
||
|
|
// 🚫 No autenticado
|
||
|
|
if (to.meta.requiresAuth && !userStore.isAuth) {
|
||
|
|
return next('/login')
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🚫 Autenticado intentando ir a login
|
||
|
|
if (to.meta.guest && userStore.isAuth) {
|
||
|
|
userStore.redirectByRole()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// 🚫 Rol requerido incorrecto
|
||
|
|
if (to.meta.role && !userStore.hasRole(to.meta.role)) {
|
||
|
|
return next('/403')
|
||
|
|
}
|
||
|
|
|
||
|
|
next()
|
||
|
|
})
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
export default router
|