# OktaAuthService.getUser() returning "undefined"

**URL:** <https://devforum.okta.com/t/oktaauthservice-getuser-returning-undefined/10090>\
**Category:** Questions\
**Tags:** angular\
**Created:** [July 10, 2020, 9:29pm UTC](https://devforum.okta.com/t/oktaauthservice-getuser-returning-undefined/10090 "2020-07-10T21:29:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisroberton](https://avatars.discourse-cdn.com/v4/letter/c/b782af/32.png) [@chrisroberton](https://devforum.okta.com/u/chrisroberton)\
**Post date:** [July 10, 2020, 9:29pm UTC](https://devforum.okta.com/t/oktaauthservice-getuser-returning-undefined/10090/1 "2020-07-10T21:29:35Z")

</div>

```
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

---

<div class="post-metadata">

**Author:** ![chrisroberton](https://avatars.discourse-cdn.com/v4/letter/c/b782af/32.png) [@chrisroberton](https://devforum.okta.com/u/chrisroberton)\
**Post date:** [July 10, 2020, 10:07pm UTC](https://devforum.okta.com/t/oktaauthservice-getuser-returning-undefined/10090/2 "2020-07-10T22:07:19Z")

</div>

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);
  }
```

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [January 24, 2024, 8:19pm UTC](https://devforum.okta.com/t/oktaauthservice-getuser-returning-undefined/10090/3 "2024-01-24T20:19:44Z")

</div>


