# getUser() fails immediately after the login redirection

**URL:** <https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240>\
**Category:** Questions\
**Created:** [December 22, 2020, 6:01pm UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240 "2020-12-22T18:01:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![lapo.martini](https://avatars.discourse-cdn.com/v4/letter/l/a3d4f5/32.png) [@lapo.martini](https://devforum.okta.com/u/lapo.martini)\
**Post date:** [December 22, 2020, 6:01pm UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/1 "2020-12-22T18:01:14Z")

</div>

Hi Guys,  
I’m pretty new to the Okta SDK…  
I’m trying to simply show the username once the user is logged in but i’m facing a strange behavior.  
If I call getUser() immediately after the user login it fails saying:

> AuthSdkError: getUserInfo requires an access token object

If i refresh the page, the getUser calls succeed and I get the username.  
Here my code:

```
console.log('Started');

if (this.oktaAuth.isAuthenticated) {

  console.log('Autenicated');
  const userClaims = await this.oktaAuth.getUser();
  console.log('User');

  this.username = userClaims.name;
}
console.log('Finish');

```

As you can see once the login redirects to the app i get this error:

 ![image](https://global.discourse-cdn.com/oktadev/original/2X/9/9abdb474289697128fe1f14c688bfe83536773b5.png)

Once i refresh the page i get everything:

 ![image](https://global.discourse-cdn.com/oktadev/original/2X/f/f5e2d1c6b46a9c6ad2c5c92ddc5e04d0d5d4f77d.png)

Any suggestion?

Thanks much!

---

<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:** [December 23, 2020, 12:09am UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/2 "2020-12-23T00:09:33Z")

</div>

Are you getting an Access Token for this user and storing it in the tokenManager when they sign in?

[getUser()](https://github.com/okta/okta-auth-js#tokengetuserinfoaccesstokenobject-idtokenobject) requires there to either be a token available in the tokenManager OR that you have passed the token object in as an argument, so if the accessToken is not available in the tokenManager, you will need to provide it when calling getUser

You can check if its available in the tokenManager by using [tokenManager.get()](https://github.com/okta/okta-auth-js#tokenmanagergetkey).

---

<div class="post-metadata">

**Author:** ![lapo.martini](https://avatars.discourse-cdn.com/v4/letter/l/a3d4f5/32.png) [@lapo.martini](https://devforum.okta.com/u/lapo.martini)\
**Post date:** [December 23, 2020, 2:29pm UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/3 "2020-12-23T14:29:51Z")

</div>

Thanks for the prompt reply!  
I’m not able to retrieve the token because the tokenManager.get is a property which returns a promise but the object seems contain a token:

 ![image](https://global.discourse-cdn.com/oktadev/original/2X/b/b3e76c83170ac6b64f09b195a86cd31cbca609e5.png)

Code:

console.log(‘Started’);

```
if (this.oktaAuth.isAuthenticated) {

  console.log('Autenticated');
  const token = await this.oktaAuth.tokenManager.get;
  console.log(token.length);
  const userClaims = await this.oktaAuth.getUser();
  console.log('User');

  this.username = userClaims.name;
}
console.log('Finish');

```

Anyway, the if condition indicate that the user is authenticated, why is there no token?

---

<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:** [December 23, 2020, 4:34pm UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/4 "2020-12-23T16:34:10Z")

</div>

Can you make sure that the token stored is an access token? It seems like the length is one, so its possible only an ID token is stored right now.

What OAuth flow are you using (Implicit or PKCE) and how/when are you storing the tokens in the token manager?

---

<div class="post-metadata">

**Author:** ![lapo.martini](https://avatars.discourse-cdn.com/v4/letter/l/a3d4f5/32.png) [@lapo.martini](https://devforum.okta.com/u/lapo.martini)\
**Post date:** [December 27, 2020, 4:13pm UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/5 "2020-12-27T16:13:28Z")

</div>

I’m using PKCE but I’m not requesting the token in any way, only using the code as explained on the official guide…

---

<div class="post-metadata">

**Author:** ![warren](https://avatars.discourse-cdn.com/v4/letter/w/97f17d/32.png) [@warren](https://devforum.okta.com/u/warren)\
**Post date:** [December 29, 2020, 6:31pm UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/6 "2020-12-29T18:31:33Z")

</div>

I agree with @andrea that you should check whether both tokens are present. If you open up the browser developer tools and check Local Storage, do you see both the idToken and accessToken in okta-token-storage?

 ![Screen Shot 2020-12-29 at 10.30.54 AM](https://global.discourse-cdn.com/oktadev/original/2X/4/4b45ec3571d852c58aeea3cb2eefb987956f6880.png)

---

<div class="post-metadata">

**Author:** ![lapo.martini](https://avatars.discourse-cdn.com/v4/letter/l/a3d4f5/32.png) [@lapo.martini](https://devforum.okta.com/u/lapo.martini)\
**Post date:** [December 30, 2020, 10:57am UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/7 "2020-12-30T10:57:43Z")

</div>

The token seems there but I still receive the error:

 ![image](https://global.discourse-cdn.com/oktadev/original/2X/f/f519be35dff1d264ffe45f08d292f46d106e00d2.png)

Any suggestion?

Thanks much!

---

<div class="post-metadata">

**Author:** ![lapo.martini](https://avatars.discourse-cdn.com/v4/letter/l/a3d4f5/32.png) [@lapo.martini](https://devforum.okta.com/u/lapo.martini)\
**Post date:** [January 8, 2021, 4:30pm UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/8 "2021-01-08T16:30:03Z")

</div>

Any clue guys?  
Thanks

---

<div class="post-metadata">

**Author:** ![lapo.martini](https://avatars.discourse-cdn.com/v4/letter/l/a3d4f5/32.png) [@lapo.martini](https://devforum.okta.com/u/lapo.martini)\
**Post date:** [January 18, 2021, 8:19am UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/9 "2021-01-18T08:19:25Z")

</div>

Anybody can help here?  
The error is systematic. I suppose the angular package has a bug in it…  
In this way the OKTA authentication is not usable because i cannot authenticate the user in the first shot…

Any suggestion is very appreciated…

---

<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 20, 2021, 4:57pm UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/10 "2021-01-20T16:57:02Z")

</div>

@lapo.martini Do you see the same behaviour when using the [sample application](https://github.com/okta/samples-js-angular)? Does it occur across different browsers, or does the behavior vary based on what browser you are using?

---

<div class="post-metadata">

**Author:** ![lapo.martini](https://avatars.discourse-cdn.com/v4/letter/l/a3d4f5/32.png) [@lapo.martini](https://devforum.okta.com/u/lapo.martini)\
**Post date:** [January 21, 2021, 11:00am UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/11 "2021-01-21T11:00:35Z")

</div>

I didn’t try the sample app, but my code is identical respect the documentation provided on OKTA website… on edge i have the same error plus an error on local storage always thrown by okta module which prevent to redirect to the authentication…

 ![image](https://global.discourse-cdn.com/oktadev/original/2X/8/8fd645b8c660245aa9cea25471fa883f6ec63b06.png)

---

<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 21, 2021, 5:19pm UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/12 "2021-01-21T17:19:38Z")

</div>

Where is getUser being called in your application? Is it on the same route that is handling the callback redirection?

If you try to [pull the accessToken out of the token manager](https://github.com/okta/okta-auth-js#tokenmanagergetkey) yourself, does that work or does it also fail?

Also what version of the AuthJS SDK are you using?

For Edge, you may want to try adding some polyfills, such as [the ones that are available in AuthJS](https://github.com/okta/okta-auth-js#browser-compatibility--polyfill), to help alleviate that “Out of stack space” error.

---

<div class="post-metadata">

**Author:** ![kmcmillan-nvisia](https://avatars.discourse-cdn.com/v4/letter/k/b19c9b/32.png) [@kmcmillan-nvisia](https://devforum.okta.com/u/kmcmillan-nvisia)\
**Post date:** [April 20, 2021, 8:53pm UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/13 "2021-04-20T20:53:09Z")

</div>

I’m seeing this same behavior. In my configuration, the OktaAuthService is being configured into an Angular service. My theory is that the constructor for my service (which calls getUser) is being called before the login/callback endpoint has consumed the token on redirect. It does however defeat the purpose of having getUser return a promise if the promise can error out, rather than waiting for the result and returning a success.

I wrapped the entire getUser call in a subscription to authentication state, and the call seems to succeed now. It does require rather a lot of gyrations, and it would be better IMO if getUser would wait for the token to be populated (since it’s already a promise), perhaps with a timeout?

Here’s the updated code that does work for my service:  
import { HttpClient } from ‘@angular/common/http’;  
import { Injectable, OnInit, EventEmitter, Output } from ‘@angular/core’;  
import { Observable } from ‘rxjs’;  
import { environment as env } from “…/…/environments/environment”;  
import { OktaAuthService } from ‘@okta/okta-angular’;

```
@Injectable({
  providedIn: 'root'
})
export class AuthenticationService {
  @Output()
  nameEmitter = new EventEmitter<String>();
  userName!: string;
  jwtError!: any;

  constructor(
    private authService: OktaAuthService
  ) {
    this.authService.$authenticationState.subscribe(
      {
        next: 
          authenticated => {
          if (authenticated) {
            this.authService.getUser()
            .then(it => {
                var name = it.name || "Guest";
                this.userName! = name;
                this.nameEmitter.emit(name);
              } 
            )
          }
        }
      }
    )
  }
}
```

---

<div class="post-metadata">

**Author:** ![Vikramalakshman\_2](https://avatars.discourse-cdn.com/v4/letter/v/4bbf92/32.png) [@Vikramalakshman\_2](https://devforum.okta.com/u/Vikramalakshman_2)\
**Post date:** [November 7, 2022, 9:40am UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/14 "2022-11-07T09:40:40Z")

</div>

> ```
> Here the Solution is
> 
> ```

I too got this exact issue and fixed the issuse in a certain amount of guesswork.The  
okta-auth-js is the storage manager of jwt token.  
In this issue, after refresh the page the getUser() call is succeed and can get the username. So I presummed that some time is needed as soon as login successful to get the call succeed . So, I fixed this issue by adding setTimeout() (_note: setTimeout() time is user preference_) in my code.

```auto
setTimeout(() => {
      this.oktaAuth.getUser().then(async (userData) => {
        this.userName = userData.name;
        this.isAuthenticated = await this.oktaAuth.isAuthenticated();
      });
    }, 400);

```

The number of setTimeout() code increases as getUser(0 call increases.So, add setTimeout() in the root component(app.component.ts) only once as following

```auto
setTimeout(() => {
// required code
    }, 400);

```

If any appropraite solution is there, please add it here.

Thanks a bunch!

---

<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 19, 2024, 12:39am UTC](https://devforum.okta.com/t/getuser-fails-immediately-after-the-login-redirection/12240/15 "2024-01-19T00:39:14Z")

</div>


