Guarded routes
In the previous section, we used a canActivate
check on a page viewmodel to ensure that users could only get to the page when they were logged in. This works, but if multiple pages need to be gated, or we need to use logic that the page might not have, it is possible to add this logic to the router.
The guardRoute
method is an optional method that the router will use to screen every attempted navigation. It receives the module being activated and the route instruction as parameters. If true
, or a promise for true, is returned from guardRoute
, then navigation continues normally. If a string or a promise for a string is returned, it will be used as a redirection route. If false
or a promise for false is returned, then navigation is cancelled:
router.guardRoute = function(model, instruction) { return !(instruction.config.auth && !dataService.isLoggedIn()); };
This router guard can replace the canActivate
method on the edit page, as it will cancel navigation when the route...