Files
banu-front/src/router/guards.js
T
sajjadtalkhabi 20551fee63 fix: bugs
2026-08-17 16:59:24 +03:30

61 lines
1.8 KiB
JavaScript

import { useAuthStore } from '@/store/auth'
import { MAIN_SITE_URL, ROLE_HOME_ROUTES } from '@/enums'
import { tokenService } from '@/services/api/token-service'
import { shouldLandOnMainSite } from '@/features/auth/composables/useRedirectAfterLogin'
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()
if (shouldLandOnMainSite(auth)) {
window.location.href = MAIN_SITE_URL
return false
}
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()
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, statusGuard, roleGuard]