This commit is contained in:
2026-05-23 02:17:37 +02:00
parent fed75d4820
commit 4f0cbe9145
9 changed files with 247 additions and 63 deletions
+34 -1
View File
@@ -5,17 +5,50 @@
*/
// Composables
import { createRouter, createWebHistory } from 'vue-router'
import {createRouter, createWebHistory} from 'vue-router'
import Index from '@/pages/index.vue'
import {useAuthStore} from '@/stores/auth'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/login',
name: 'login',
component: () => import('@/pages/login.vue'),
},
{
path: '/',
component: Index,
},
{
path: '/admin',
name: 'admin-dashboard',
component: () => import('@/pages/admin/Dashboard.vue'),
meta: {requiresAdmin: true}
},
],
})
router.beforeEach((to, from, next) => {
const authStore = useAuthStore()
const publicPages = ['login', 'register']
const authRequired = !publicPages.includes(to.name as string)
const adminRequired = to.matched.some(record => record.meta.requiresAdmin)
if (authRequired && !authStore.isAuthenticated) {
// Non connecté -> Login
next('/login')
} else if (to.name === 'login' && authStore.isAuthenticated) {
// Déjà connecté -> Accueil
next('/')
} else if (adminRequired && !authStore.isAdmin) {
// Connecté mais pas admin -> Accueil (ou page 403)
next('/')
} else {
next()
}
})
export default router