Auth.currentSession() returning 'No current User' intermittently

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
valentin7commented, Nov 17, 2019

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!

4reactions
victorS7Pcommented, Jul 14, 2020

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:

if (user.challengeName === 'NEW_PASSWORD_REQUIRED') {
   Auth.completeNewPassword(user, 'NEW_PASSWORD')
  .then(() => {
    this.handleCurrentSession()
   })
} else {
  this.handleCurrentSession()
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

aws-amplify auth currentSession returns no current user
It looks like I cannot get user session, unless it is wrapped inside Auth.signIn. That's the Node/JS async programming model in a nutshell....
Read more >
AWS-Amplify/Lobby - Gitter
My current direction is maybe Nested types in ElasticSearch, but I'm not sure how that plays into graphql schema / the filter ......
Read more >
Authentication - Advanced workflows - JavaScript - Amplify Docs
federatedSignIn() to get AWS credentials directly from Cognito Federated Identities and not use User Pool federation. If you have logged in with Auth....
Read more >
Load the State from the Session - SST.Dev
The Auth.currentSession() method throws an error No current user if nobody is currently logged in. We don't want to show this error to...
Read more >
Flask-Login — Flask-Login 0.7.0 documentation
By default, Flask-Login uses sessions for authentication. ... This property should return True if the user is authenticated, i.e. they have provided valid ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found