-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(router): Update router docs to use guard functions instead of @I…
…njectable (#47989) This commit updates various router docs to use the functional style guards instead of the `@Injectable` style. PR Close #47989
- Loading branch information
1 parent
a93a1a5
commit 2efcc26
Showing
26 changed files
with
242 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
aio/content/examples/router/src/app/auth/auth-routing.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,8 @@ | ||
// #docregion | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | ||
import { CanActivateFn } from '@angular/router'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AuthGuard implements CanActivate { | ||
canActivate( | ||
route: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot): boolean { | ||
console.log('AuthGuard#canActivate called'); | ||
return true; | ||
} | ||
} | ||
export const authGuard: CanActivateFn = () => { | ||
console.log('authGuard#canActivate called'); | ||
return true; | ||
}; | ||
// #enddocregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,25 @@ | ||
// #docregion | ||
import {Injectable} from '@angular/core'; | ||
import {inject} from '@angular/core'; | ||
import { | ||
ActivatedRouteSnapshot, CanActivate, CanMatch, | ||
Route, Router, RouterStateSnapshot, UrlTree | ||
CanActivateFn, CanMatchFn, | ||
Router, UrlTree | ||
} from '@angular/router'; | ||
|
||
import {AuthService} from './auth.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AuthGuard implements CanActivate, CanMatch { | ||
constructor(private authService: AuthService, private router: Router) {} | ||
export const authGuard: CanMatchFn|CanActivateFn = () => { | ||
const authService = inject(AuthService); | ||
const router = inject(Router); | ||
|
||
canActivate( | ||
route: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot): true|UrlTree { | ||
const url: string = state.url; | ||
|
||
return this.checkLogin(url); | ||
} | ||
|
||
// #enddocregion | ||
// #docregion can-match | ||
canMatch(route: Route) { | ||
const url = `/${route.path}`; | ||
return this.checkLogin(url) === true; | ||
if (authService.isLoggedIn) { | ||
return true; | ||
} | ||
// #enddocregion can-match | ||
// #docregion | ||
|
||
checkLogin(url: string): true|UrlTree { | ||
if (this.authService.isLoggedIn) { | ||
return true; | ||
} | ||
const url = router.getCurrentNavigation()!.extractedUrl.toString(); | ||
// Store the attempted URL for redirecting | ||
authService.redirectUrl = url; | ||
// Redirect to the login page | ||
return router.parseUrl('/login'); | ||
}; | ||
|
||
// Store the attempted URL for redirecting | ||
this.authService.redirectUrl = url; | ||
|
||
// Redirect to the login page | ||
return this.router.parseUrl('/login'); | ||
} | ||
} | ||
// #enddocregion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,23 @@ | ||
// #docregion can-activate-child | ||
import { Injectable } from '@angular/core'; | ||
import { inject } from '@angular/core'; | ||
import { | ||
CanActivate, Router, | ||
ActivatedRouteSnapshot, | ||
RouterStateSnapshot, | ||
CanActivateChild, | ||
CanActivateFn, CanMatchFn, Router, | ||
CanActivateChildFn, | ||
UrlTree | ||
} from '@angular/router'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AuthGuard implements CanActivate, CanActivateChild { | ||
constructor(private authService: AuthService, private router: Router) {} | ||
export const authGuard: CanMatchFn|CanActivateFn|CanActivateChildFn = () => { | ||
const authService = inject(AuthService); | ||
const router = inject(Router); | ||
|
||
canActivate( | ||
route: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot): true|UrlTree { | ||
const url: string = state.url; | ||
|
||
return this.checkLogin(url); | ||
} | ||
|
||
canActivateChild( | ||
route: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot): true|UrlTree { | ||
return this.canActivate(route, state); | ||
if (authService.isLoggedIn) { | ||
return true; | ||
} | ||
|
||
// #enddocregion can-activate-child | ||
checkLogin(url: string): true|UrlTree { | ||
if (this.authService.isLoggedIn) { return true; } | ||
|
||
// Store the attempted URL for redirecting | ||
this.authService.redirectUrl = url; | ||
|
||
// Redirect to the login page | ||
return this.router.parseUrl('/login'); | ||
} | ||
// #docregion can-activate-child | ||
} | ||
// #enddocregion | ||
const url = router.getCurrentNavigation()!.extractedUrl.toString(); | ||
// Store the attempted URL for redirecting | ||
authService.redirectUrl = url; | ||
// Redirect to the login page | ||
return router.parseUrl('/login'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,38 @@ | ||
// #docplaster | ||
// #docregion | ||
import { Injectable } from '@angular/core'; | ||
import { inject } from '@angular/core'; | ||
import { | ||
CanActivate, Router, | ||
ActivatedRouteSnapshot, | ||
RouterStateSnapshot, | ||
CanActivateChild, | ||
CanActivateFn, CanMatchFn, Router, | ||
CanActivateChildFn, | ||
NavigationExtras, | ||
UrlTree | ||
} from '@angular/router'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AuthGuard implements CanActivate, CanActivateChild { | ||
constructor(private authService: AuthService, private router: Router) {} | ||
export const authGuard: CanMatchFn|CanActivateFn|CanActivateChildFn = () => { | ||
const authService = inject(AuthService); | ||
const router = inject(Router); | ||
|
||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): true|UrlTree { | ||
const url: string = state.url; | ||
|
||
return this.checkLogin(url); | ||
if (authService.isLoggedIn) { | ||
return true; | ||
} | ||
|
||
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): true|UrlTree { | ||
return this.canActivate(route, state); | ||
} | ||
const url = router.getCurrentNavigation()!.extractedUrl.toString(); | ||
|
||
checkLogin(url: string): true|UrlTree { | ||
if (this.authService.isLoggedIn) { return true; } | ||
// Store the attempted URL for redirecting | ||
authService.redirectUrl = url; | ||
|
||
// Store the attempted URL for redirecting | ||
this.authService.redirectUrl = url; | ||
// Create a dummy session id | ||
const sessionId = 123456789; | ||
|
||
// Create a dummy session id | ||
const sessionId = 123456789; | ||
// Set our navigation extras object | ||
// that contains our global query params and fragment | ||
const navigationExtras: NavigationExtras = { | ||
queryParams: { session_id: sessionId }, | ||
fragment: 'anchor' | ||
}; | ||
|
||
// Set our navigation extras object | ||
// that contains our global query params and fragment | ||
const navigationExtras: NavigationExtras = { | ||
queryParams: { session_id: sessionId }, | ||
fragment: 'anchor' | ||
}; | ||
// Redirect to the login page with extras | ||
return router.createUrlTree(['/login'], navigationExtras); | ||
}; | ||
|
||
// Redirect to the login page with extras | ||
return this.router.createUrlTree(['/login'], navigationExtras); | ||
} | ||
} |
Oops, something went wrong.