Skip to content

Commit b3ad53f

Browse files
authored
Merge pull request #987 from damienbod/fabiangosebrink/improve-auto-login
Fabiangosebrink/improve auto login
2 parents 4c0a3b9 + 499d882 commit b3ad53f

File tree

64 files changed

+596
-756
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+596
-756
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Angular Lib for OpenID Connect/OAuth2 Changelog
22

3+
### 2021-02-27 Version 11.6.1
4+
5+
- Added AutoLoginGuard
6+
- [PR](https://github.com/damienbod/angular-auth-oidc-client/pull/987)
7+
- Updated Azure AD, Azure B2C templates to prompt for select_account (problem with multiple accounts)
8+
39
### 2021-02-24 Version 11.6.0
410

511
- Added support for OAuth Pushed authorisation requests (PAR)

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,15 @@ or with yarn
7878

7979
- [Guards](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/guards.md)
8080
- [Features](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/features.md)
81+
8182
- [Public Events](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/features.md#public-events)
8283
- [Auth with a popup](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/authorizing-popup.md)
8384
- [Custom Storage](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/features.md#custom-storage)
8485
- [Custom parameters](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/features.md#custom-parameters)
86+
- [Auto Login](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/features.md#auto-login)
8587
- [Using the OIDC package in a module or a Angular lib](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/features.md#using-the-oidc-package-in-a-module-or-a-angular-lib)
8688
- [Delay the loading or pass an existing AuthWellKnownEndpoints config](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/features.md#delay-the-loading-or-pass-an-existing-well-knownopenid-configuration-configuration)
89+
8790
- [Logout](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/logout.md)
8891
- [Using and revoking the access token](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/using-access-tokens.md)
8992
- [CSP & CORS](https://github.com/damienbod/angular-auth-oidc-client/tree/main/docs/csp-cors-config.md)

docs/features.md

+23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [Public Events](#public-events)
44
- [Custom Storage](#custom-storage)
55
- [Custom parameters](#custom-parameters)
6+
- [Auto Login](#auto-login)
67
- [Using the OIDC package in a module or a Angular lib](#using-the-oidc-package-in-a-module-or-a-angular-lib)
78
- [Delay the loading or pass an existing AuthWellKnownEndpoints config](#delay-the-loading-or-pass-an-existing-well-knownopenid-configuration-configuration)
89

@@ -88,6 +89,28 @@ Then provide the class in the module:
8889
})
8990
```
9091

92+
## Auto Login
93+
94+
If you want to have your app being redirected to the sts automatically without the user clicking any login button you can use the `AutoLoginGuard` provided by the lib. Use it for all the routes you want automatic login to be enabled.
95+
96+
If you are using auto login _make sure_ to _*not*_ call the `checkAuth()` method in your `app.component.ts`. This will be done by the guard automatically for you.
97+
98+
Sample routes could be
99+
100+
```typescript
101+
import { AutoLoginGuard } from 'angular-auth-oidc-client';
102+
103+
const appRoutes: Routes = [
104+
{ path: '', pathMatch: 'full', redirectTo: 'home' },
105+
{ path: 'home', component: HomeComponent, canActivate: [AutoLoginGuard] },
106+
{ path: 'protected', component: ProtectedComponent, canActivate: [AutoLoginGuard] },
107+
{ path: 'forbidden', component: ForbiddenComponent, canActivate: [AutoLoginGuard] },
108+
{ path: 'unauthorized', component: UnauthorizedComponent },
109+
];
110+
```
111+
112+
[src code](../projects/sample-code-flow-auto-login)
113+
91114
## Custom parameters
92115

93116
Custom parameters can be added to the auth request by adding them to the config you are calling the `withConfig(...)` method with. They are provided by

docs/guards.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ import { map } from 'rxjs/operators';
1111

1212
@Injectable({ providedIn: 'root' })
1313
export class AuthorizationGuard implements CanActivate {
14-
constructor(private oidcSecurityService: OidcSecurityService, private router: Router) {}
15-
16-
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
17-
return this.oidcSecurityService.isAuthenticated$.pipe(
18-
map((isAuthorized: boolean) => {
19-
console.log('AuthorizationGuard, canActivate isAuthorized: ' + isAuthorized);
20-
21-
if (!isAuthorized) {
22-
this.router.navigate(['/unauthorized']);
23-
return false;
24-
}
25-
26-
return true;
27-
})
28-
);
29-
}
14+
constructor(private oidcSecurityService: OidcSecurityService, private router: Router) {}
15+
16+
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
17+
return this.oidcSecurityService.isAuthenticated$.pipe(
18+
map((isAuthorized: boolean) => {
19+
console.log('AuthorizationGuard, canActivate isAuthorized: ' + isAuthorized);
20+
21+
if (!isAuthorized) {
22+
this.router.navigate(['/unauthorized']);
23+
return false;
24+
}
25+
26+
return true;
27+
})
28+
);
29+
}
3030
}
3131
```
3232

docs/logout.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Logout
22

3-
The `logoff()` function sends an endsesion request to the OIDC server, if it is available, or the check session has not sent a changed event.
3+
The `logoff()` function sends an end session request to the OIDC server, if it is available, or the check session has not sent a changed event.
44

55
```typescript
66
logout() {

0 commit comments

Comments
 (0)