+17
-2
@@ -1,3 +1,4 @@
|
||||
import { ROLE_HOME_ROUTES } from '@/enums'
|
||||
import { useAuthStore } from '@/store/auth'
|
||||
import { tokenService } from '@/services/api/token-service'
|
||||
|
||||
@@ -18,11 +19,25 @@ 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 { name: ROLE_HOME_ROUTES[auth.role] || 'student-dashboard' }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Students whose account is not yet `approved` (pending/verified) must finish
|
||||
// the registration wizard before reaching any authenticated page.
|
||||
export const statusGuard = (to) => {
|
||||
if (isPublic(to)) return
|
||||
if (to.meta?.requiresGuest) return
|
||||
if (to.name === 'student-register') return
|
||||
if (!tokenService.hasToken()) return
|
||||
const auth = useAuthStore()
|
||||
if (!auth.user) return
|
||||
if (auth.role && auth.role !== 'student') return
|
||||
if (auth.user.status === 'approved') return
|
||||
return { name: 'student-register' }
|
||||
}
|
||||
|
||||
export const roleGuard = (to) => {
|
||||
if (!to.meta?.role) return
|
||||
const auth = useAuthStore()
|
||||
@@ -37,4 +52,4 @@ export const titleGuard = (to) => {
|
||||
document.title = title ? `${APP_TITLE} - ${title}` : APP_TITLE
|
||||
}
|
||||
|
||||
export const guards = [authGuard, guestGuard, roleGuard]
|
||||
export const guards = [authGuard, guestGuard, statusGuard, roleGuard]
|
||||
|
||||
+7
-7
@@ -15,13 +15,13 @@ const router = createRouter({
|
||||
},
|
||||
})
|
||||
|
||||
// router.beforeEach((to, from) => {
|
||||
// for (const guard of guards) {
|
||||
// const result = guard(to, from)
|
||||
// if (result !== undefined) return result
|
||||
// }
|
||||
// return
|
||||
// })
|
||||
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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user