Skip to content

Route Guards

Route guards control whether a user can navigate to or away from a route. They’re essential for authentication, authorization, and data protection.

  • CanActivate: Can user enter this route?
  • CanDeactivate: Can user leave this route? (unsaved changes)
  • CanMatch: Should this route match?
  • CanActivateChild: Protect all child routes
export const authGuard: CanActivateFn = (route, state) => {
const authService = inject(AuthService);
const router = inject(Router);
if (authService.isLoggedIn()) return true;
return router.createUrlTree(['/login']);
};

Guards are essential for controlling navigation access in Angular applications.”