Excuse my French, but here's my issue:
AppComponent.ts
import { Component, OnInit, HostListener } from '@angular/core';
import { environment } from '../environments/environment';
import { AuthOidcService, AuthUser } from '@cddng/auth-oidc-apim';
import { AuthService } from './service/auth.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
appName: string;
authenticatedUser: AuthUser;
constructor(private auth: AuthService, private oidcAuth: AuthOidcService) {
this.appName = environment.application_name;
this.oidcAuth.init(environment.auth_oidc_config);
}
ngOnInit() {
this.oidcAuth.login();
this.oidcAuth.oidcSecurityService.getIsAuthorized().subscribe((authorized: boolean) => {
if (authorized) {
this.authenticatedUser = this.oidcAuth.getAuthenticatedUserFromJwt();
}
});
this.auth.idRh = this.authenticatedUser.idRh;
this.auth.role = this.authenticatedUser.roles;
console.log("*******************************************");
console.log("idrh => " + this.auth.idRh);
console.log("Roles => " + this.auth.role);
console.log("isAdmin => " + this.auth.isAdmin);
console.log("isExpert => " + this.auth.isExpert);
console.log("isConsult => " + this.auth.isConsultation);
console.log("*******************************************");
}
@HostListener('window:UserDisconnected') userDisconnected() {
this.oidcAuth.corpHeaderLogout();
}
}
The following error appears on the console:
ERROR TypeError: "this.authenticatedUser is undefined"
Refreshing the page resolves the issue, but not on first load
I suspect it's an asynchronous problem, but I'm unable to find a solution
Your insights are greatly appreciated