BarberApp uses functional Angular route guards to protect routes. All guards are located in src/app/core/guards/ and use dependency injection to access facades and services.
All guards are implemented as async functions to properly wait for authentication state checks.
Protects routes based on user role, with automatic redirection to the correct dashboard.Location: src/app/core/guards/role.guard.ts:6Type: Factory function returning CanActivateFn
export const roleGuard = (allowedRoles: UserRoles[]) => { return async () => { const authFacade = inject(AuthFacade); const router = inject(Router); while (authFacade.isCheckingAuth()) { await new Promise(resolve => setTimeout(resolve, 10)); } const user = authFacade.user(); if (!user) { router.navigate(['/auth/login']); return false; } if (allowedRoles.includes(user.role)) { return true; } else { // Redirect to the corresponding dashboard according to the user's role switch (user.role) { case UserRoles.CLIENT: router.navigate(['/dashboard/client']); break; case UserRoles.SPECIALIST: router.navigate(['/dashboard/specialist']); break; case UserRoles.ADMIN: router.navigate(['/dashboard/specialist']); break; default: router.navigate(['/dashboard/client']); } return false; } };};
Protects appointment detail routes, ensuring users can only access their own appointments.Location: src/app/core/guards/id.guard.ts:6Type: CanActivateFn with route parameter
Protects medical record routes, ensuring only the client or specialists can view records.Location: src/app/core/guards/record.guard.ts:6Type: CanActivateFn with route parameter
export const recordGuard = ( route: ActivatedRouteSnapshot): boolean => { const authFacade = inject(AuthFacade); const router = inject(Router); const user = authFacade.user(); const clientIdFromRoute = route.paramMap.get('id'); if (!user || !clientIdFromRoute) { router.navigate(['/']); return false; } // A specialist can see any client's record. if (user.role === UserRoles.SPECIALIST) { return true; } // A client can only see their own record. if (user.role === UserRoles.CLIENT) { if (user.id === clientIdFromRoute) { return true; } } router.navigate(['/dashboard/client']); return false;};