Sitemap
Ace D Baugh

Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer

Angular Route Guards

3 min readMar 10, 2023
Press enter or click to view image in full size
Photo by Henry Be on Unsplash

Route guards are a feature in Angular that allows developers to control access to certain routes in the application. They are used to ensure that only authorized users can access certain routes, and to prevent unauthorized users from accessing sensitive or restricted areas of the application.

The most basic route guard in Angular is the canActivate function. This function is used to determine if a user is allowed to access a particular route. It is typically used to implement authentication and authorization checks. For example, a developer might use the canActivate function to ensure that only logged-in users can access a certain route.

Besides Angular’s canActivate function, there are several other functions available for use as route guards. These include:

canActivateChild: This function is similar to the canActivate function, but it is used to determine if a user is allowed to access child routes of a particular route. This function is useful for ensuring that a user has access to all of the routes within a certain area of the application.

canDeactivate: This function is used to determine if a user is allowed to navigate away from a particular route. This function is typically used to prevent a user from navigating away from a page without saving changes or confirming that they want to leave.

Get Ace D Baugh’s stories in your inbox

Join Medium for free to get updates from this writer.

canLoad: This function is used to determine if a user is allowed to lazy-load a particular module. This function is typically used to ensure that a user has access to a certain area of the application before the module is loaded.

These functions work by returning a boolean value, indicating whether or not the user is allowed to access the route. If the function returns true, the user is allowed to access the route. If the function returns false, the user is not allowed to access the route, and they will be redirected to a different route.

When deciding which route guard to use, it is important to consider the specific requirements of the application. For example, if the application requires authentication and authorization checks, the canActivate function is a good choice. If the application needs to prevent a user from navigating away from a page without saving changes, the canDeactivate function is a good choice. And, if the application needs to ensure that a user has access to a certain area of the application before a module is loaded, the canLoad function is a good choice.

In conclusion, Route guards are a feature in Angular that allows developers to control access to certain routes in the application. They are used to ensure that only authorized users can access certain routes, and to prevent unauthorized users from accessing sensitive or restricted areas of the application. Angular provides several functions for use as route guards, including canActivate, canActivateChild, canDeactivate, and canLoad. Each function serves a specific purpose, and the choice of which function to use depends on the specific requirements of the application.

References

Alligator.io. (2021, June 14). How to use route guards with angular router. DigitalOcean. Retrieved January 16, 2023, from https://www.digitalocean.com/community/tutorials/angular-route-guards

Chenkie, R. (2019, July 9). Angular authentication: Using route guards. Medium. Retrieved January 16, 2023, from https://medium.com/@ryanchenkie_40935/angular-authentication-using-route-guards-bf7a4ca13ae3

--

--

Ace D Baugh
Ace D Baugh

Written by Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer