Sub topic 4
What a guard can return
This chapter assumes you can write a functional guard with inject() and attach it to a route with canActivate. It covers the values a guard may return and what the router does with each one.
The return type
Every functional guard type (CanActivateFn, CanActivateChildFn, CanDeactivateFn, CanMatchFn) returns a GuardResult or an asynchronous wrapper around one. A GuardResult is one of three things:
| Value | What the router does |
|---|---|
| `true` | Lets the navigation continue. |
| `false` | Cancels the navigation. The user stays on the current page. |
| `UrlTree` | Cancels the navigation and starts a new one to that URL. |
| `RedirectCommand` (Angular 18 and later) | Same as a `UrlTree`, and also lets you pass navigation options such as `replaceUrl`. |
A guard may also return an Observable or a Promise of any of these values. That lets it wait for a server call or a stored session before it decides.
Choosing between false and a redirect
Return false when there is nothing useful to show instead. This fits a canDeactivate guard where the user chose to stay. Return a UrlTree when the user should end up somewhere else, such as a login page. A bare false on a first page load can leave the user on a blank screen, because there is no previous page to stay on.
Asynchronous results
An observable guard can check a stream of the current user. The router takes the first value the observable emits, so the stream does not have to complete.
import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { Observable, map } from 'rxjs';
// Minimal stand-in for your own service.
class AuthService {
user$!: Observable<{ name: string } | null>;
}
export const sessionGuard: CanActivateFn = () => {
const router = inject(Router);
return inject(AuthService).user$.pipe(
map(user => (user ? true : router.parseUrl('/login'))),
);
};In a real app, AuthService would be an @Injectable class that you import. The guard returns either true or a UrlTree in the same stream, so the map callback has the type boolean | UrlTree.
A guard can also be an async function. It then returns a Promise<boolean | UrlTree>, which the router handles the same way.
Redirecting with options
RedirectCommand wraps a UrlTree and adds navigation options.
import { inject } from '@angular/core';
import { CanActivateFn, RedirectCommand, Router } from '@angular/router';
export const replaceGuard: CanActivateFn = () => {
const router = inject(Router);
const allowed = false; // replace with a real check
return allowed
? true
: new RedirectCommand(router.parseUrl('/login'), { replaceUrl: true });
};With replaceUrl: true, the login page replaces the blocked URL in the browser history. The Back button then does not return the user to a page that would redirect them again.
Common errors
- Returning
undefinedby forgetting areturnin one branch. The type checker flags it, but a plain JavaScript guard will not. - Calling
router.navigate()inside a guard and then returningfalse. This starts a second navigation by hand. Return theUrlTreeinstead, so the router handles both steps as one navigation. - Returning an observable that never emits. The navigation then hangs with no error.
Exercise
A guard returns an observable that emits true after 100 ms and then keeps emitting new values every second without completing. Does the navigation finish, and which value does the router use?
Show answer
The navigation finishes. The router uses the first emitted value, true, and ignores later ones. The observable does not need to complete.