Auth.currentSession() returning 'No current User' intermittently
See original GitHub issueDescribe the bug
Context: Angular.io application using cognito user pool and dynamodb.
Everything works find most of the time, and I cannot reproduce the error at will. However, other users are receiving errors in production and we’re capturing them through our implementation of rollbar.
We’ve tracked it down to the following code:
try {
const cognitoUserSession: CognitoUserSession = await Auth.currentSession();
const idToken = cognitoUserSession.getIdToken();
return idToken.getJwtToken();
} catch (e) {
// dump local storage for review
const localStorage = window.localStorage;
for (const key of Object.keys(localStorage)) {
const value = localStorage[key].substring(0, 20);
this.messageService.add(`${key} ${value}`);
}
throw new Error(`Auth.currentSession - Error ${e}`);
}
Intermittently Auth.currentSession() throws an error: ‘No current user’
I’ve reviewed the amplity-js code, for the error message. It seems to happen when the current user cannot be retrieved from the window.local storage.
/** * Get current authenticated user * @return - A promise resolves to current authenticated CognitoUser if success */ public currentUserPoolUser(params?: CurrentUserOpts): Promise<CognitoUser | any> { if (!this.userPool) { return this.rejectNoUserPool(); } const that = this; return new Promise((res, rej) => { this._storageSync.then(() => { const user = that.userPool.getCurrentUser(); if (!user) { logger.debug(‘Failed to get user from user pool’); rej(‘No current user’); return; }
// refresh the session if the session expired.
user.getSession((err, session) => {
if (err) {
logger.debug('Failed to get the user session', err);
rej(err);
return;
}
However, in my error handler I’ve logged the key pairs in local storage and they exist.
I’m looking for any help as to what might be causing this. Thanks again for you help.
Desktop (please complete the following information):
- OS: Windows
- Browser chrome
- Version 76
You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = ‘DEBUG’; in your app.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:16 (1 by maintainers)
Top Related StackOverflow Question
Hi - I’ve also been getting this same error in the way that @singulli1 describes. Been following/subscribed to this thread in case solutions came up — but seems the stale bot might close this soon.
@singulli1 were you able to figure out what was causing this error for you? Agreed that changing how Amplify handles error handling here would be useful to get a more specific signal of what might be going wrong.
Let me know if there’s any workarounds or fixes found. Thanks!
I had this problem because I was trying to recover the session of a user who still did not have a password set (it was created by the admin on the aws console)
My solution: