Angular oauth2 OIDC

install oauth2 in Angular

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

importing the NgModule

app.module.ts

login / logout - app.component.ts

app.component.ts

login / logout - app.component.html

app-routing.module.ts

References

Last updated

Was this helpful?