import { Injectable } from '@angular/core'; import { OktaAuth } from '@okta/okta-auth-js'; import { OKTA_AUTH } from '@okta/okta-angular'; import { Inject } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class AuthService { isAuthenticatedData: any; isAuthenticated: any; emp_id: any; level: any; ops: any; vendor: any; constructor( @Inject(OKTA_AUTH) private oktaAuth: OktaAuth ) { } async isUserLoggedIn(): Promise { const authState = await this.oktaAuth.authStateManager.getAuthState(); const myCookieValue = decodeURIComponent( this.getCookie('okta-token-storage_accessToken') ); const cookieObject = JSON.parse(myCookieValue); // console.log(cookieObject.claims.groups); if (authState) { if (authState.accessToken != null) { this.emp_id = authState.accessToken.claims['employeeNumber']; } return !!this.emp_id; } else if (cookieObject.accessToken) { return true; } else { return false; } } getCookie(name: string): string { var val = ""; const cookies = document.cookie.split(';'); for (const cookie of cookies) { const [key, value] = cookie.trim().split('='); if (key === name) { return value; } } return val; } }