npm i angular-oauth2-oidc --save
npm i angular-oauth2-oidc-jwks --save
add service --AuthGuard
\app ng g service shared/services/authguard --skip-tests
import { Injectable } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthguardService implements CanActivate {
constructor(
private oauthService: OAuthService,
private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
// Check to see if we have an application user
// If we dont, we navigate to [/]
if (this.oauthService.hasValidAccessToken()) {
return true;
}
this.router.navigate(['/']);
return false;
};
}
add service
ng g service shared/services/globals --skip-tests
add service
ng g service shared/services/http.service --skip-tests
Documentation:
https://openbase.com/js/angular-oauth2-oidc/documentation
Community-provided sample implementation
https://github.com/manfredsteyer/angular-oauth2-oidc
Angular Authentication with OpenID Connect and Okta in 20 Minutes
https://developer.okta.com/blog/2017/04/17/angular-authentication-with-oidc
Example angular-oauth2-oidc with AuthGuard
https://github.com/jeroenheijmans/sample-angular-oauth2-oidc-with-auth-guards/
Configuring for Implicit Flow
https://manfredsteyer.github.io/angular-oauth2-oidc/docs/additional-documentation/using-implicit-flow.html