init
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user