Getting "no current user" after successful login to Cognito UserPool
See original GitHub issueWhen 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:
- Created 5 years ago
- Reactions:5
- Comments:34 (3 by maintainers)
Top GitHub Comments
I had the same problem, but for me, removing the cookie storage configuration in
aws-exports.js
solved it. Maybe this helps someone.I think the recommended solution is to give up on Cognito/Amplify and use something that works.