Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(router): Update router guide to use UrlTree for guard redirects #37100

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions aio/content/examples/router/src/app/auth/auth.guard.2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// #docregion
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, UrlTree } from '@angular/router';

import { AuthService } from './auth.service';

Expand All @@ -12,21 +12,20 @@ export class AuthGuard implements CanActivate {

canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
state: RouterStateSnapshot): true|UrlTree {
let url: string = state.url;

return this.checkLogin(url);
}

checkLogin(url: string): boolean {
checkLogin(url: string): true|UrlTree {
if (this.authService.isLoggedIn) { return true; }

// Store the attempted URL for redirecting
this.authService.redirectUrl = url;

// Navigate to the login page with extras
this.router.navigate(['/login']);
return false;
// Redirect to the login page
return this.router.parseUrl('/login');
}
}
// #enddocregion
Expand Down
14 changes: 7 additions & 7 deletions aio/content/examples/router/src/app/auth/auth.guard.3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
CanActivate, Router,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanActivateChild
CanActivateChild,
UrlTree
} from '@angular/router';
import { AuthService } from './auth.service';

Expand All @@ -16,28 +17,27 @@ export class AuthGuard implements CanActivate, CanActivateChild {

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
state: RouterStateSnapshot): true|UrlTree {
let url: string = state.url;

return this.checkLogin(url);
}

canActivateChild(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
state: RouterStateSnapshot): true|UrlTree {
return this.canActivate(route, state);
}

// #enddocregion can-activate-child
checkLogin(url: string): boolean {
checkLogin(url: string): true|UrlTree {
if (this.authService.isLoggedIn) { return true; }

// Store the attempted URL for redirecting
this.authService.redirectUrl = url;

// Navigate to the login page
this.router.navigate(['/login']);
return false;
// Redirect to the login page
return this.router.parseUrl('/login');
}
// #docregion can-activate-child
}
Expand Down
14 changes: 7 additions & 7 deletions aio/content/examples/router/src/app/auth/auth.guard.4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanActivateChild,
NavigationExtras
NavigationExtras,
UrlTree
} from '@angular/router';
import { AuthService } from './auth.service';

Expand All @@ -16,17 +17,17 @@ import { AuthService } from './auth.service';
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(private authService: AuthService, private router: Router) {}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): true|UrlTree {
let url: string = state.url;

return this.checkLogin(url);
}

canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): true|UrlTree {
return this.canActivate(route, state);
}

checkLogin(url: string): boolean {
checkLogin(url: string): true|UrlTree {
if (this.authService.isLoggedIn) { return true; }

// Store the attempted URL for redirecting
Expand All @@ -42,8 +43,7 @@ export class AuthGuard implements CanActivate, CanActivateChild {
fragment: 'anchor'
};

// Navigate to the login page with extras
this.router.navigate(['/login'], navigationExtras);
return false;
// Redirect to the login page with extras
return this.router.createUrlTree(['/login'], navigationExtras);
}
}
6 changes: 3 additions & 3 deletions aio/content/guide/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -2737,7 +2737,7 @@ If the user is logged in, it returns true and the navigation continues.
The `ActivatedRouteSnapshot` contains the _future_ route that will be activated and the `RouterStateSnapshot` contains the _future_ `RouterState` of the application, should you pass through the guard check.

If the user is not logged in, you store the attempted URL the user came from using the `RouterStateSnapshot.url` and tell the router to redirect to a login page—a page you haven't created yet.
This secondary navigation automatically cancels the current navigation; `checkLogin()` returns `false`.
Returning a `UrlTree` tells the `Router` to cancel the current navigation and schedule a new one to redirect the user.

{@a add-login-component}

Expand Down Expand Up @@ -2790,8 +2790,8 @@ Extend the `AuthGuard` to protect when navigating between the `admin` routes.
Open `auth.guard.ts` and add the `CanActivateChild` interface to the imported tokens from the router package.

Next, implement the `canActivateChild()` method which takes the same arguments as the `canActivate()` method: an `ActivatedRouteSnapshot` and `RouterStateSnapshot`.
The `canActivateChild()` method can return an `Observable<boolean>` or `Promise<boolean>` for async checks and a `boolean` for sync checks.
This one returns a `boolean`:
The `canActivateChild()` method can return an `Observable<boolean|UrlTree>` or `Promise<boolean|UrlTree>` for async checks and a `boolean` or `UrlTree` for sync checks.
This one returns either `true` to allow the user to access the admin feature module or `UrlTree` to redirect the user to the login page instead:

<code-example path="router/src/app/auth/auth.guard.3.ts" header="src/app/auth/auth.guard.ts (excerpt)" region="can-activate-child"></code-example>

Expand Down