import { Injectable } from '@angular/core'; import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, } from '@angular/router'; import { Observable } from 'rxjs'; // for debugging import { OktaAuthService } from '@okta/okta-angular'; import { environment } from './../../environments/environment'; import Utils from './utils'; import { SharedService } from './shared.service'; import { UserService } from '../services/user.service'; @Injectable({providedIn:'root'}) export class AuthGuardSerivce implements CanActivate { private COMPONENT_NAME: string = 'AuthGuardSerivce'; constructor(public router: Router, public oktaAuth: OktaAuthService, public sharedService: SharedService, public userService: UserService) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean { const METHOD_NAME: string = 'canActivate()'; // console.log("this.router.url>>>>"+ this.router.url); let isAuthenticated: boolean = true; if (!this.userService.getUser()) { isAuthenticated = false; this.sharedService.setAppDefault(); this.sharedService.clearSessionStorage(true); this.sharedService.clearLocalStorage(); //this.router.navigate(['login']); this.redirectLoginPage();//redirecting to login page } return isAuthenticated; } async redirectLoginPage(): Promise { if(environment.oktaEnable) { await this.oktaAuth.logout(); this.oktaAuth.loginRedirect('/login') }else{ this.router.navigate(['login']); } } }