acquireTokensilent return null accessToken randomly.
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report [ ] Performance issue [ ] Feature request [ ] Documentation issue or request [ x] Other… Please describe: Technical doubt and possibly a bug.
Browser:
- [ X] Chrome
- Firefox version XX
- IE version XX
- Edge version XX
- Safari version XX
Library version
< Library version: 1.0.0
Current behavior
accessToken from acquireTokenSilent is null sometimes
Expected behavior
accessToken from acquireTokenSilent is never null. Any error should come in the error function.
Minimal reproduction of the problem with instructions
I have an apihelper that every time is going to do a call contacts my clientApplication wrapper like this:
getAuthorizationCookie = async function () {
var token = await Ab2c.getTokenAsync()
if (!token)
{
console.log(token);
}
return 'Bearer ' + token;
}
The implementation of Ab2c.getTokenAsync is this:
let authCallback = function (error, response) {
if (error)
console.log(error);
};
const clientApplication = new UserAgentApplication(config);
clientApplication.handleRedirectCallback(authCallback);
//FIXME: this temporary catch token is wrong and should be removed. It is here as a workaround because ab2c to sometimes returns a valid response with a null token.
let tempCacheToken="";**
class Ab2c {
....
static getTokenAsync = async () => {
let tokenRequest = {
scopes: config.auth.b2cScopes,
authority: config.auth.authority,
extraQueryParameters: config.auth.extraQueryParameters
};
return new Promise((resolve,reject) =>{
clientApplication.acquireTokenSilent(tokenRequest).then(function (loginResponse) {
if (!loginResponse.accessToken)
{
**//This If happens sometimes when I refresh the page**
console.log("null token");
loginResponse.accessToken = tempCacheToken;
}
else
{
console.log("valid token")
tempCacheToken = loginResponse.accessToken;
}
return resolve(loginResponse.accessToken);
}).catch(function (error) {
console.log(error);
Ab2c.logout()
let newError = new Error(error);
newError.fromMsal = true;
return reject(newError);
});
});
}
....
}
So, basically I’m relying in clientApplication to handle the cached token for me since I understood that’s what I’m supposed to do when using msal. But sometimes when I hit F5 in my page I get a valid loginResponse with a null accessToken.
When I refresh I do like 4 or 5 different requests which means 4 or 5 different calls to acquiretokensilent. What I see is that sometimes all of them work and sometimes the first actually works and returns a token but the following return a null value, most of the times all of them work…
Log example.
valid token null token valid token valid token valid token valid token
I think it might be related to refreshing while some request is still pending but I can’t prove it.
Any ideas?
Miguel
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:42 (13 by maintainers)
Top GitHub Comments
@mmuarc What I did was:
npm install
andnpm run build
in the/lib/msal-core
foldernpm uninstall msal
to remove the npm hosted packagenpm install file:<path_to_msal-core_folder>
to install the package locallyThough this may be different than what others may do, this is what enabled me to test the new version.
Don’t forget to remove the reference to the local package by running
npm uninstall msal
and thennpm install msal
again when you are done testing.Hope this was helpful to you.
@mmuarc @raikoss @sfigie Thanks for your patience. I have pushed a fix in PR #768. Please feel free to pull this branch and let me know if this fix works. I will push this as a patch as soon as I have verified that it works for you.