import { ROLE_HOME_ROUTES } from '@/enums' import { useAuthStore } from '@/store/auth' import { tokenService } from '@/services/api/token-service' 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 { 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.roles.includes('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.roles.includes(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]