question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting "no current user" after successful login to Cognito UserPool

See original GitHub issue

When I call Auth.currentSession() I always get “no current user” even though I’m logged in successfully.

I’m using AWS Amplify and AWS Gateway API in a React Native app.

We already have a Cognito UserPool and API set up and working with an existing web-based UI. I’m now trying to create a mobile app using React Native and Amplify. So first off, I created a config that pointed at our User Pool and created a custom login page that called Auth.signIn like this:

const data = await Auth.signIn(username, password);
console.log(`onSignIn::Response#1: ${JSON.stringify(data, null, 2)}`);

This works in that the call succeeds and returns a load of data including a JWT token.

In fact I’ve found that if I store away this token and then later add it to my headers, I can call my API. I do it like this:

await AsyncStorage.setItem(
  'userToken',
   data.signInUserSession.idToken.jwtToken
);

But if I try and get the current session as per the documentation, like this:

Auth.currentAuthenticatedUser()
  .then(user => console.log('>>>>Authenticated user:', user))
  .catch(err => console.log('>>>>Auth user error', err));

Then it always gets caught with “no current user”. Switching on the AWS logging shows that there’s no federated info and I get log messages like the following:

[11:50:17] [DEBUG] 50:17.48 AuthClass - Getting current user credentials
[11:50:17] [DEBUG] 50:17.50 AuthClass - failed to get or parse item aws-amplify-federatedInfo [SyntaxError: JSON Parse error: Unexpected identifier "undefined"]
[11:50:17] [DEBUG] 50:17.51 AuthClass - Getting current session

We didn’t have a Cognito Identity Pool, just a User Pool. I thought this might be the problem, so I created an Identity Pool and updated the config. No difference.

I thought maybe the way I’ve set them up was wrong, so next I tried creating a new app and using the amplify cli, I called amplify add auth. That created a new user pool and identity pool and created a file locally with all the IDs. So next I changed my app to use the ids for this new user and identity pool. No difference, still no current user.

Not giving up just yet, I then decided that maybe I wasn’t quite logging in correctly, so I switched to using the withAuthenticator helper that comes with aws-amplify-react-native. This seemed to work! When I launched the app I got the AWS-provided log-in screen and when I typed in the right credentials, I got in to my app. However, if I try and get the currentSession, it still fails with the same error.

I’ve read through https://github.com/aws-amplify/amplify-js/issues/500 and it seems that IdentityPools shouldn’t be required, but as far as I can tell, the aws code still fails if there isn’t an identity pool.

My workaround at the moment is to store the token away using AsyncStorage and then get it out again to add it to the header, here’s my config:

{
  Auth: {
    userPoolId: 'eu-west-2xxxx',
    userPoolWebClientId: 'yyyy',
    region: 'eu-west-2',
    cookieStorage: {
      domain: '.example.com'
    }
  },
  API: {
    endpoints: [
      {
        name: 'ExampleAPI',
        endpoint: 'https://example.com',
        region: 'eu-west-2',
        custom_header: async () => {
          const token = await AsyncStorage.getItem('userToken');
          return {
            Authorization: `Bearer ${token}`
          };
        }
      }
    ]
  }
}

This works fine until the token has expired. I don’t know how to fix that, I was hoping that the Auth functionality would sort that for me.

Where am I going wrong?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:34 (3 by maintainers)

github_iconTop GitHub Comments

45reactions
pieterjandesmedtcommented, Jul 4, 2019

I had the same problem, but for me, removing the cookie storage configuration in aws-exports.js solved it. Maybe this helps someone.

42reactions
cwellscommented, Aug 9, 2020

I think the recommended solution is to give up on Cognito/Amplify and use something that works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No current user for authenticated user in Amplify
I see the default Sign In form for Amplify, I use it to enter username and password and then click on "SIGN IN"...
Read more >
Signing up and confirming user accounts - Amazon Cognito
The user has successfully signed up, but cannot sign in until the user account is confirmed. The user is enabled but not confirmed...
Read more >
amazon-cognito-identity-js - npm
Your User Pool in Amazon Cognito is a fully managed user directory that can scale to hundreds of millions of users, so you...
Read more >
Authentication in React with AWS Cognito and Amplify
aws/credentials . After successful setup, you'll get a confirmation that a new user has been successfully set up: React app setup. I prefer ......
Read more >
User authentication with AWS Cognito: The good, the bad and ...
And last and for me the worst issue: No backups. If your user pool with 10 million users gets deleted, it's deleted. Game...
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