Implementing a Route Guard

Let's explore how we can use a route guard to check if the user is logged in before going to the route.

Step 1: Create a TypeScript class

To implement a route guard, we need to create a TypeScript class that uses one or more of these route interfaces. By implementing an interface, we need to have a function in our class that the interface says we should have.

So, if we implement the CanActivate interface, we need to have a canActivate() function in our class. This function returns true or false. If true, the user can access the route; if false, then they can’t.

Get hands-on with 1200+ tech skills courses.