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:
Are you getting an Access Token for this user and storing it in the tokenManager when they sign in?
getUser() 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().
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:
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?
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…
@lapo.martini Do you see the same behaviour when using the sample application? Does it occur across different browsers, or does the behavior vary based on what browser you are using?
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…
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’;
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.
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
setTimeout(() => {
// required code
}, 400);
If any appropraite solution is there, please add it here.