Inconsistent behaviour of fetchAuthSession and getCurrentUser for a session expired user
Describe the bug On Android, for a user with session expired
- Calling
Amplify.Auth.fetchAuthSession()will returnAuthSessionwithisSignedIn = true. - Calling
Amplify.Auth.getCurrentUser()throwsSignedOutException. - Calling
Amplify.Auth.fetchAuthSession(options: CognitoSessionOptions(getAWSCredentials: true))throwsSessionExpiredException
To Reproduce
- Set the expiration time of refresh token to be 60 mins.
- Open app, log in, wait till session expired, now call these APIs again and observe the results.
Expected behaviour Their behaviours should be consistent with each other.
Also, I wish there could be a documentation on how to properly handle session expiration. For example:
- Should we sign out the user (before showing the user with sign in screen) when session expired?
- If we didn't sign out the user, and the user close the app and the open again, do we still expect a
SESSION_EXPIREDevent from hub.
Platform Amplify Flutter current supports iOS and Android. This issue is reproducible in (check all that apply): [X] Android [] iOS
Smartphone (please complete the following information):
- Device: OnePlus Nord
- OS: Android 10
- Browser: Chrome
- Version: Amplify 0.1.0
Another thing that I'm observing is that the future Amplify.Auth.fetchUserAttributes() will not finish if the session is expired. This behaviour is only seen on Android as well.
Any updates on this issue?
@fanwgwg Thanks for posting this issue. We're investigating.
@haverchuck Have you found any solution?
@trinanda98 This will require a change in one of our underlying native amplify libraries. We are working to resolve this discrepancy.
@haverchuck The underlying issue https://github.com/aws-amplify/amplify-android/issues/1247 seems not getting attention, do you know if it's been looked into or is being investigated by relevant team?
Thank you!
any updates on the issue?
Any updates now?
Still experiencing this exact issue as of today.
@fanwgwg @cconley717 @borainceler @trinanda98 : Apologies for the lack of communication on this thread. We are currently working on a fix to resolve platform discrepencies within the APIs which was alluded to here. We are exploring a couple of solutions for the currentUser disparity, as well as some enhancements to our documentation to make all of this more clear!
@haverchuck Has this issue been solved?
@fanwgwg @trinanda98 @yukitaka13-1110 @cconley717 @borainceler -
We have published a preview which should address these issues. If you can, please pull amplify-flutter packages with version 0.3.0-rc.1 and give it a try!
Is this the same thing? https://github.com/aws-amplify/amplify-flutter/issues/201
@kjones No - this particular getCurrentUser issue describes a platform discrepancy wherein iOS returns an AuthUser when the session has expired, and Android does not. Now, both platforms return an AuthUser.
Was anyone able to try the fix provided by @haverchuck in version 0.3.0-rc.1?
EDIT: since this point is worth its own issue, I've opened a new issue for it. Also leaving the comment here because I believe it could be related to this issue and may possibly shed a light on it.
I tried it -- at least I was able to replace the packages in pubspec.yaml from version 0.2.10 to 0.3.0-rc.1. (The packages I had upgraded were: amplify_api, amplify_auth_cognito and amplify_flutter). After that upgrade, I still had the exact same issue with getCurrentUser(). The issue is probably the same as the one reported above or related to it. The issue is that:
immediately after Amplify.signIn() is called, the Amplify library is unable to fetch the tokens, so when I call Amplify.getCurrentUser() right after the call to signIn() it throws an exception (SignedOutException) even though the Amplify.signIn() call has just returned result.isSignedIn == true and result.nextStep!.signInStep == 'DONE'.
Changing to 0.3.0-rc.1 fixed absolutely nothing - the same problem continues. Nobody can sign in a user into their app right now (unless I'm misunderstanding something).
I am using the emulator in all this (Android) as the web version of Amplify-Flutter does not yet exist. The emulator shows some errors and warnings from its Android/Java side that may be illuminating in this case, so I'll include them. The discussion above talks about calls to Amplify.signIn() and immediately after that to Amplify.getCurrentUser() and how they differ in what they report.
Here are the Android/Java console messages after calling Amplify.signIn() and before (not yet) calling Amplify.getCurrentUser():
D/AWSMobileClient(13077): _federatedSignIn: Putting provider and token in store
D/AWSMobileClient(13077): Inspecting user state details
D/AWSMobileClient(13077): hasFederatedToken: false provider: cognito-idp.us-east-1.amazonaws.com/us-east-1_Bk5hz4hAS
W/AWSMobileClient(13077): Failed to federate tokens during sign-in
W/AWSMobileClient(13077): java.lang.RuntimeException: Error in federating the token.
W/AWSMobileClient(13077): at com.amazonaws.mobile.client.AWSMobileClient$10.run(AWSMobileClient.java:1826)
W/AWSMobileClient(13077): at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)
W/AWSMobileClient(13077): at com.amazonaws.mobile.client.AWSMobileClient.federatedSignInWithoutAssigningState(AWSMobileClient.java:1754)
W/AWSMobileClient(13077): at com.amazonaws.mobile.client.AWSMobileClient$6$1.onSuccess(AWSMobileClient.java:1243)
W/AWSMobileClient(13077): at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getSession(CognitoUser.java:1023)
W/AWSMobileClient(13077): at com.amazonaws.mobile.client.AWSMobileClient$6.run(AWSMobileClient.java:1228)
W/AWSMobileClient(13077): at com.amazonaws.mobile.client.internal.InternalCallback$1.run(InternalCallback.java:101)
W/AWSMobileClient(13077): at java.lang.Thread.run(Thread.java:764)
W/AWSMobileClient(13077): at com.amazonaws.auth.CognitoCachingCredentialsProvider.refresh(CognitoCachingCredentialsProvider.java:511)
W/AWSMobileClient(13077): at com.amazonaws.auth.CognitoCachingCredentialsProvider.getIdentityId(CognitoCachingCredentialsProvider.java:453)
W/AWSMobileClient(13077): at com.amazonaws.auth.CognitoCredentialsProvider.populateCredentialsWithCognito(CognitoCredentialsProvider.java:785)
W/AWSMobileClient(13077): at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:703)
W/AWSMobileClient(13077): at com.amazonaws.auth.CognitoCredentialsProvider.refresh(CognitoCredentialsProvider.java:640)
W/AWSMobileClient(13077): at com.amazonaws.auth.CognitoCachingCredentialsProvider.refresh(CognitoCachingCredentialsProvider.java:511)
W/AWSMobileClient(13077): at com.amazonaws.mobile.client.AWSMobileClient.federateWithCognitoIdentity(AWSMobileClient.java:1857)
W/AWSMobileClient(13077): at com.amazonaws.mobile.client.AWSMobileClient$10.run(AWSMobileClient.java:1813)
W/AWSMobileClient(13077): ... 7 more
As you can see, the complaints false provider: cognito-idp.us-east-1.amazonaws.com/us-east-1_Bk5hz4hAS
and Failed to federate tokens during sign-in are probably at the root of this problem.
By the way - all of the above code used to work for me in past versions - in other words, after calling signIn() the immediate call to getCurrentUser() used to return a new user instead of throwing the SignedOutException.
NOTE: I did make sure that the values in amplifyconfiguration.dart for identity pool ID and for the user pool ID and app client ID connected to it are correct - they are all correct: they are exactly the same as the ones in the AWS console when you go to 'Edit identity pool' - there is nothing wrong with the values of these endpoints in the amplifyconfiguration.dart.
Hello - It looks like this was addressed in 0.3.0. If you are still facing this issue, please let us know. Otherwise we will close this out.
Closing the issue out. Feel free to open another issue if you are still facing a problem.