first commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { tokenService } from '@/services/api/token-service'
|
||||
import { useAuthStore } from '@/store/auth'
|
||||
|
||||
const APP_TITLE = 'بنو'
|
||||
|
||||
const isPublic = (to) => to.meta?.public === true
|
||||
|
||||
export const authGuard = (to) => {
|
||||
if (isPublic(to)) return
|
||||
if (to.meta?.requiresGuest) return
|
||||
if (!tokenService.hasToken()) {
|
||||
return { name: 'login', query: { redirect: to.fullPath } }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
export const guestGuard = (to) => {
|
||||
if (!to.meta?.requiresGuest) return
|
||||
if (tokenService.hasToken()) {
|
||||
const auth = useAuthStore()
|
||||
return auth.role === 'admin' ? { name: 'admin-dashboard' } : { name: 'student-dashboard' }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
export const roleGuard = (to) => {
|
||||
if (!to.meta?.role) return
|
||||
const auth = useAuthStore()
|
||||
if (auth.role !== to.meta.role) {
|
||||
return { name: 'home' }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
export const titleGuard = (to) => {
|
||||
const title = to.meta?.title
|
||||
document.title = title ? `${APP_TITLE} - ${title}` : APP_TITLE
|
||||
}
|
||||
|
||||
export const guards = [authGuard, guestGuard, roleGuard]
|
||||
@@ -0,0 +1,28 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
|
||||
import { guards, titleGuard } from '@/router/guards'
|
||||
|
||||
const routeModules = import.meta.glob('@/features/**/router.js', { eager: true })
|
||||
const routes = Object.values(routeModules).flatMap((mod) => mod.default || [])
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes: [...routes, { path: '/:pathMatch(.*)*', redirect: { name: 'home' } }],
|
||||
scrollBehavior(to, from, savedPosition) {
|
||||
if (savedPosition) return savedPosition
|
||||
if (to.hash) return { el: to.hash }
|
||||
return { top: 0 }
|
||||
},
|
||||
})
|
||||
|
||||
// router.beforeEach((to, from) => {
|
||||
// for (const guard of guards) {
|
||||
// const result = guard(to, from)
|
||||
// if (result !== undefined) return result
|
||||
// }
|
||||
// return
|
||||
// })
|
||||
|
||||
router.afterEach((to) => titleGuard(to))
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user