What are route guards and why are they used?

Use route guards to prevent users from navigating to parts of an app without authorization

Route Guards are used to secure the route paths

In most cases, the routes and screens are protected behind a good authentication system

The route guard resolves to true or false based on custom logic and functionality

We can generate any number of guards based on our application requirements

To generate the route guard, we can make use of angular CLI

ng generate guard <guard-name>

Inject the guard in our module under providers

There are various types of route guards available

  • CanActive dash: checks to see if a user can visit a route.

  • CanActiveChild dash: checks to see if a user can visit a routes children.

  • CanDeactivate dash: checks to see if a user can exit a route.

  • Resolve dash: performs route data retrieval before route activation.

  • CanLoad dash: checks to see if a user can route to a module that lazy loaded.

The route guard resolves to true or false based on custom logic and functionality