|
|
|
|
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',
|
|
|
|
|
component: () => import('../views/administrador/layout/Layout.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'administrador' },
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
path: '/admin/dashboard',
|
|
|
|
|
name: 'Dashboard',
|
|
|
|
|
component: () => import('../views/administrador/Dashboard.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'administrador' }
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
path: '/admin/dashboard/areas',
|
|
|
|
|
name: 'Areas',
|
|
|
|
|
component: () => import('../views/administrador/areas/AreasList.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'administrador' }
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
path: '/admin/dashboard/cursos',
|
|
|
|
|
name: 'Cursos',
|
|
|
|
|
component: () => import('../views/administrador/cursos/CursosList.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'administrador' }
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
path: '/admin/dashboard/cursos/:id/preguntas',
|
|
|
|
|
name: 'CursoPreguntas',
|
|
|
|
|
component: () => import('../views/administrador/cursos/PreguntasCursoView.vue'),
|
|
|
|
|
meta: { requiresAuth: true }
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
path: '/admin/dashboard/procesos',
|
|
|
|
|
name: 'Procesos',
|
|
|
|
|
component: () => import('../views/administrador/Procesos/ProcesosList.vue'),
|
|
|
|
|
meta: { requiresAuth: true }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
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
|