- Overview
- Transcript
6.7 Using Route Guards
In this lesson you'll learn how we can protect routes using route guards. There are a number of different route guards that we can choose from, but in this lesson we'll focus on the canActivate
guard.
1.Introduction6 lessons, 42:00
1.1Introduction00:48
1.2Get Started With Angular-CLI11:09
1.3Developing With Angular-CLI13:17
1.4TypeScript vs. JavaScript06:54
1.5Angular Modules From the CLI04:31
1.6CLI Options05:21
2.Get Started With Angular7 lessons, 42:38
2.1Bootstrapping the Application04:30
2.2The Application Module04:15
2.3The Application Component08:06
2.4Component Styling03:06
2.5Global Styling05:11
2.6Creating a Component With the CLI09:34
2.7Creating a Service With the CLI07:56
3.Core Concepts7 lessons, 55:20
3.1Component Trees06:20
3.2Dependency Injection06:52
3.3Content Projection05:38
3.4Component and Directive Lifecycle Methods06:31
3.5Component-Only Lifecycle Methods05:28
3.6Decorators07:36
3.7Models16:55
4.Template Deep Dive11 lessons, 1:10:56
4.1Basic Data Binding With Interpolation05:35
4.2Property Bindings07:07
4.3Attribute Bindings03:29
4.4Event Bindings08:16
4.5Class and Style Bindings05:44
4.6The `NgClass` and `NgStyle` Directives05:04
4.7The `*ngIf` Directive04:41
4.8The `*ngFor` Directive09:29
4.9Inputs05:33
4.10Using Pipes in a Template07:31
4.11Using Pipes in a Class08:27
5.Forms10 lessons, 1:45:41
5.1Handling User Input With Template Reference Variables07:06
5.2Template-Driven Forms11:10
5.3Template-Driven Forms: Validation and Submission14:00
5.4Reactive Forms11:26
5.5Using a `FormBuilder`08:01
5.6Reactive Validation With Built-in Validators14:53
5.7Creating Custom Validators for Template-Driven Forms12:18
5.8Creating Custom Validators for Reactive Forms08:26
5.9Observing Form State Changes12:40
5.10Working With the `@HostListener` Decorator05:41
6.Routing9 lessons, 1:15:10
6.1Defining and Configuring Routes07:53
6.2Rendering Components With Router Outlets10:14
6.3Using Router Links for Navigation05:25
6.4Navigating Routes Using the Router06:24
6.5Determining the Active Route Using an Activated Route07:16
6.6Working With Route Parameters10:42
6.7Using Route Guards07:36
6.8Observing Router Events10:55
6.9Adding Child Routes08:45
7.Using the HTTP Client5 lessons, 56:24
7.1Sending an HTTP Request10:52
7.2Handling an HTTP Response11:22
7.3Setting Request Headers12:33
7.4Intercepting Requests09:04
7.5Finishing the Example Application12:33
8.Testing10 lessons, 1:23:27
8.1Service Unit Test Preparation10:45
8.2Unit Testing Services13:24
8.3Component Unit Test Preparation12:35
8.4Unit Testing Components07:27
8.5Unit Testing Component Templates06:58
8.6Unit Testing Pipes04:41
8.7Unit Testing Directives04:56
8.8Unit Testing Validators04:48
8.9Unit Testing Observables11:37
8.10Unit Testing HTTP Interceptors06:16
9.Building for Production1 lesson, 03:40
9.1Building for Production03:40
10.Conclusion1 lesson, 01:32
10.1Conclusion01:32
6.7 Using Route Guards
Hi folks, in this lesson we're going to take a look at route guards. Route guards are incredibly useful for testing whether or not a route can be loaded or unloaded, and this is exactly what we need right now. At the moment, as you might have noticed while we have been testing, one big problem that we have is that we can navigate straight to the game component without going to the start component first. So, we are currently on the start component. And if we click the button without entering a name, the validation will stop us navigating to the game component. But if we come up to the URL here, and if we just try to navigate straight to the game component, that works and we get an error in the console, so that's not great. It would be much better if users couldn't go to the game component if they haven't come from the start component. This is exactly the kind of thing that route guards are for. Let's see how we can use one to fix our current predicament. There are a number of different types of guards that we can use including CanActivate, CanDeactivate, CanActivateChild and CanLoad. We can use the CanActivate guard. Let's get started. Guards are typically implemented as services that we can inject into other services or components. Let's generate one now. We can put the new service in the _services folder because technically it is a service. We could create an _guards folder or similar if we were going to have a lot of them, but we aren't. So let's just put it in the generic services folder. We'll need to import the actual guard that we want to use. And in this case that's going to be the CanActivate guard. We'll also need the PlayerService here. The PlayerGuardService class will need to implement the CanActivate interface. Great, now, in order to implement the CanActivate interface correctly, the class should have a CanActivate method. We should also inject the PlayerService into the constructor. So Angular will invoke the CanActivate method to check whether or not the route can be activated. And the method should return either truth if the route can be activated or false if it cannot. We can just check the length of the players array in the PlayerService. If it's less than 2, we know the game component has not been accessed correctly. So now to use our new guard, first of all, we'll need to import the guard into our route module. And we can then add the service to the providers array. Now we need to add the guard to our root configuration, which is in app.routes.ts. Again, we'll need to import the guard service first. Route objects have a property called CanActivate, they have properties for all of the different guards. So, we can add our guard using this property and we want to add it to the game route The value of the canActivate guard property is an array. Okay, so let's test things out. So we're currently on the start screen. Let's see if we can navigate directly to the game screen. We can't go straight to the game screen now. The browser just stops loading at the root of the site. Good, so the guard is working. We can no longer go straight to the game component. We just need to arrange things so that the user is redirected to the start component instead of just hanging with nothing loaded in the home component. We can do this in the guard itself. We'll need to bring in the router to use the navigate method. So now before we return folks from the guard, we can navigate back to the start route. So let's try it out in the browser. So we're back on the Start screen. And if we try to go straight to the game component without first starting a game correctly, we'll just get navigated back to the start component again, awesome. So one point to note is that the guard we've built today is a synchronous guard, it doesn't need to do any additional processing or make any Ajax requests or anything else asynchronous. Guards often will need to be asynchronous, especially the CanActivate guard. Like when it is used to check, for example, that a user is logged in or something like that. In this scenario, instead of returning a boolean, the guard can return an observable instead. Routes can then subscribe to the observable to be notified when the guard does finally signal that the route can be activated or not. We still specified this type of guard in our route definition in the same way. We don't have to do anything special. So creating an asynchronous guard is almost just as easy as creating a regular synchronous one, like we have today. So, in this lesson, we saw how we can implement guards on routes, so that routes can only be accessed in certain situations. We saw that a route guard is really just a simple service which implements one of the guard interfaces. We saw that we should return true if the route may be accessed, or false if it should not. And we saw how to redirect the user to another part of the application in the case that the route cannot be activated. And we learned that we can also use asynchronous guards, we just need to return an observable instead of a boolean. Thanks for watching.