liveBook · Manning

Route guard

As explained in Modern Angular

An Angular routing guard is a router hook that controls whether navigation or route activation may proceed.

A route guard controls whether navigation or route activation proceeds, commonly restricting enterprise pages according to a user's authentication status. section 3.3 section 3.3.1

The older approach defines an injectable class implementing CanActivate. Its canActivate method returns a boolean or an UrlTree redirect, while injected services provide authentication state and router navigation. section 3.3.1

Modern Angular can discard the class and express the same behavior as a CanActivateFn. The inject function preserves dependency injection while reducing boilerplate and improving composability. section 3.3.1 section 1.4.3

Protecting HRMS employee pages

An authGuard injects AuthService and Router, observes isAuth$, and maps authenticated users to true; unauthenticated users receive a URL tree for /login. Register it with canActivate: [authGuard] on the employees route, including a lazy-loaded child route.

How Modern Angular covers it

How deep it goes
Explains class-based and functional implementations, dependency injection, observable decisions, redirects, and route registration.
Examples
Uses an HRMS AuthGuard that redirects unauthenticated users to /login and protects a lazy-loaded employees route.
What it assumes
Assumes familiarity with Angular building blocks and dependency injection while contrasting older class-based patterns with newer function-based composition.
What you can do after
Build it from scratch as a class-based or functional authentication guard and attach it to an Angular route.
How it connects
Connects route guards with dependency injection, authentication, routing, observables, lazy loading, and class-based versus functional architecture.

Also explained in

Compare how 2 books explain route guard →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern Angular. The section links point to the parts of the book it draws on.