import { OktaAuthService } from '@okta/okta-angular';
...
constructor(public oktaAuth: OktaAuthService) {}
...
async ngOnInit() {
const userClaims = await this.oktaAuth.getUser();
this.username = userClaims.name;
Why would I be getting “Uncaught (in promise): TypeError: Cannot read property ‘name’ of undefined” if this route is loaded immediately after the user successfully logs in?
I feel like I must be making a dumb mistake or else this would be a huge bug, right? Any help would be awesome.
Thanks!
Chris
Because it’s related, I’ll share the following block of code from my app.component.ts. It’s producing output like so:
Inside constructor for AppComponent
Constructor for AppComponent finished and isAuthenticated - undefined
AppComponent init called.
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
AppComponent init finished - user is authenticated?false
export class AppComponent{
isAuthenticated: boolean;
constructor(public oktaAuth: OktaAuthService) {
console.log("Inside constructor for AppComponent");
this.oktaAuth.$authenticationState.subscribe(
(isAuthenticated: boolean) => this.isAuthenticated = isAuthenticated
);
console.log("Constructor for AppComponent finished and isAuthenticated - " + this.isAuthenticated);
}
async ngOnInit() {
console.log("AppComponent init called.");
this.isAuthenticated = await this.oktaAuth.isAuthenticated();
console.log("AppComponent init finished - user is authenticated?" + this.isAuthenticated);
}