Skip to content

Commit 1dca183

Browse files
Merge pull request #1231 from fmalcher/docuguard
docs(guards): use UrlTree for redirect, clean up
2 parents 962e7a9 + e6119fe commit 1dca183

File tree

1 file changed

+18
-13
lines changed
  • docs/site/angular-auth-oidc-client/docs/documentation

1 file changed

+18
-13
lines changed
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
2-
sidebar_label: Using Guards
2+
sidebar_label: Using Route Guards
33
sidebar_position: 7
44
---
55

6-
# Using Guards
6+
# Using Route Guards
77

8-
> The guard should only be applied to protected URLs. The guard should not be active on the default route, where the authorization request is processed.
8+
> Guards should only be applied to protected URLs. There should be no guard active on the default route where the authorization request is processed.
99
1010
Please refer to the auto-login guard in this repo as a reference. It is important that the callback logic can be run on a route without the guard running or run before the guard logic.
1111

1212
```ts
1313
import { Injectable } from '@angular/core';
14-
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
14+
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
1515
import { OidcSecurityService } from 'angular-auth-oidc-client';
1616
import { Observable } from 'rxjs';
1717
import { map } from 'rxjs/operators';
@@ -20,28 +20,33 @@ import { map } from 'rxjs/operators';
2020
export class AuthorizationGuard implements CanActivate {
2121
constructor(private oidcSecurityService: OidcSecurityService, private router: Router) {}
2222

23-
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
23+
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> {
2424
return this.oidcSecurityService.isAuthenticated$.pipe(
2525
map(({ isAuthenticated }) => {
26-
console.log('AuthorizationGuard, canActivate isAuthenticated: ' + isAuthenticated);
27-
28-
if (!isAuthenticated) {
29-
this.router.navigateByUrl('/unauthorized');
30-
return false;
26+
// allow navigation if authenticated
27+
if (isAuthenticated) {
28+
return true;
3129
}
3230

33-
return true;
31+
// redirect if not authenticated
32+
return this.router.parseUrl('/unauthorized');
3433
})
3534
);
3635
}
3736
}
3837
```
3938

40-
Do not forget to add the guard to your routes with `canActivate`, `canLoad`, etc.
39+
To apply the guard for specific routes you have to add it to the route configuration e.g. with `canActivate`:
4140

4241
```ts
4342
const appRoutes: Routes = [
43+
{
44+
path: 'protected',
45+
component: <yourComponent>,
46+
canActivate: [AuthorizationGuard]
47+
},
4448
// ...
45-
{ path: 'protected', component: <yourComponent>, canActivate: [AuthorizationGuard] }
4649
];
4750
```
51+
52+
All other guard types like `canLoad` or `canActivateChild` work in a similar way. However, the guard class has to implement the respective interfaces and methods accordingly.

0 commit comments

Comments
 (0)