amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

Inconsistent behaviour of fetchAuthSession and getCurrentUser for a session expired user

Open fanwgwg opened this issue 5 years ago • 18 comments

Describe the bug On Android, for a user with session expired

  1. Calling Amplify.Auth.fetchAuthSession() will return AuthSession with isSignedIn = true.
  2. Calling Amplify.Auth.getCurrentUser() throws SignedOutException.
  3. Calling Amplify.Auth.fetchAuthSession(options: CognitoSessionOptions(getAWSCredentials: true)) throws SessionExpiredException

To Reproduce

  1. Set the expiration time of refresh token to be 60 mins.
  2. 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:

  1. Should we sign out the user (before showing the user with sign in screen) when session expired?
  2. If we didn't sign out the user, and the user close the app and the open again, do we still expect a SESSION_EXPIRED event 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

fanwgwg avatar Mar 12 '21 10:03 fanwgwg

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.

fanwgwg avatar Mar 13 '21 11:03 fanwgwg

Any updates on this issue?

fanwgwg avatar Mar 18 '21 02:03 fanwgwg

@fanwgwg Thanks for posting this issue. We're investigating.

haverchuck avatar Mar 22 '21 20:03 haverchuck

@haverchuck Have you found any solution?

trinanda98 avatar Mar 25 '21 14:03 trinanda98

@trinanda98 This will require a change in one of our underlying native amplify libraries. We are working to resolve this discrepancy.

haverchuck avatar Mar 26 '21 17:03 haverchuck

@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!

fanwgwg avatar Apr 27 '21 16:04 fanwgwg

any updates on the issue?

borainceler avatar May 04 '21 17:05 borainceler

Any updates now?

fanwgwg avatar Jun 10 '21 03:06 fanwgwg

Still experiencing this exact issue as of today.

cconley717 avatar Jun 10 '21 22:06 cconley717

@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 avatar Jul 19 '21 23:07 haverchuck

@haverchuck Has this issue been solved?

yukitaka13-1110 avatar Sep 02 '21 14:09 yukitaka13-1110

@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!

haverchuck avatar Sep 24 '21 21:09 haverchuck

Is this the same thing? https://github.com/aws-amplify/amplify-flutter/issues/201

kjones avatar Sep 24 '21 21:09 kjones

@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.

haverchuck avatar Sep 24 '21 21:09 haverchuck

Was anyone able to try the fix provided by @haverchuck in version 0.3.0-rc.1?

dnys1 avatar Nov 17 '21 16:11 dnys1

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.

dorontal avatar Dec 11 '21 19:12 dorontal

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.

Jordan-Nelson avatar Mar 14 '22 17:03 Jordan-Nelson

Closing the issue out. Feel free to open another issue if you are still facing a problem.

Jordan-Nelson avatar Mar 21 '22 19:03 Jordan-Nelson