From the course: Angular: Building Large Applications

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Protecting routes with guards

Protecting routes with guards

- [Instructor] Now, let's talk about protecting routes with guards in Angular. When building Angular apps, there are often routes that should only be accessible to certain users. For example, you might want to protect an admin page from non-admin users or restrict access to certain routes unless the user is logged in. That's where Angular's route guards come in. Guards are services that will allow us to control access to specific routes based on certain conditions. Angular provides several types of route guards, such as CanActivate, CanDeactivate, Resolve, CanLoad, and CanActivateChild. Each serves a unique purpose for protecting your routes. So, let's start by focusing on the most commonly used guard, CanActivate. This guard is used to prevent navigation to a route unless a condition is met. For example, we can use it to check if the user is authenticated. First, we need to create a guard service, on the left. Here, the AuthGuard checks if the user is logged in by calling a method in…

Contents