Getting logged out in React Native #7944
Comments
|
We are experiencing the exact same issue, with a similar setup, except we store the token in the Let me know if I can assist with anything to get this progressing. |
|
Sorry you are experiencing issues! Would you be able to provide a larger code snippet or perhaps a basic sample repo that shows this behavior? I'd like to reproduce this, but I haven't seen this behavior before with a React Native app I have configured with federation. I'm mainly interested in where/how Also, have you reproduced this behavior without passing in a custom storage solution? Amplify will use |
|
@amhinson Does the following help? Pretty sure we were able to reproduce it without passing any storage implementation yes, then we did pass one because of the following issue. Seems sessions problems can arise from |
|
I'm having this same issue. Here's how I reproduce in an emulator:
I'm using a custom storage object as described in the Amplify docs. It turns out that the storage object isn't loading all the data it needs from memory before we try to validate the session. So the order of events is:
I'm toying around with different solutions now. The most promising so far is to add a slight delay (100ms) before the first call to Auth.currentSession() to give the sync() method time to complete. |
|
@nerdygirl Thanks for the explanation. Just to make sure we're talking about the same issue: |
|
Yeah, the same thing happens to me in that case. And although I swear this was working on both Android and iOS previously, it now only appears to work on Android. The difference seems to be that the keys stored in AsyncStorage are now wiped out on iOS, even if I'm just refreshing the app. Are you noticing any differences between Android and iOS, @samih7? |
|
@nerdygirl Both occurring on iOS and Android for me. I just got the issue while debugging our app, I logged out the storage content and got the following: So it really looks like user-related data has been cleared out from the storage. @amhinson Edit: once again, |
|
Same issue. Using own implementation of storage based on SecureStore (expo-secure-store). No issues on Amplify auth v2, started happening on v3. |
|
I have a new theory I'm testing out. Hoping it's helpful to someone (or you can also test and we can share notes). I followed instructions from AWS Cognito docs to write my custom storage class. Here's the code they provide for In my code, I replaced this line: with a call to One million tests and console.log statements later, I believe that what happens is this:
So to fix, I either need to store the keys—and just the keys, not the values—in AsyncStorage or I need to store them somewhere else and retrieve them in the A quick test with storing them in AsyncStorage works locally, but I haven't tried it on a device yet. I just added this to the top of Probably not what I'll go with when I ship it, but at least I don't have to log in again every time I refresh the app on my emulator for right now. Tomorrow, I'll figure out a good way to store these keys in Keychain for consistency. UPDATE: In a major face-palm moment, I figured out why I thought I had it working previously. It's because I tested on my emulator with the default storage. Since that's AsyncStorage, all the keys did get into AsyncStorage. So then when I re-added my custom storage class, it worked! So exciting! Except it was all lies. The fix I mentioned on April 4 is still needed because of the way I'm querying my own backend service for user data. You can see my confusion on April 8 when it suddenly was only working on one emulator. I now believe that's because I spun up a new iOS emulator so it stopped working there, but kept working on Android. |
|
@nerdygirl @rcCaregiven @Goszu @therealemjy Are you guys also reproducing the issue on v4.x.x? |
|
@samih7 v4 blows up with Expo:
|
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
Not stale bot, still looking into this. Thank you @Goszu, @samih7, and @nerdygirl for your patience and the details you've provided us thus far. Apologies for the delay. |
|
Thanks everyone for all the details. I am looking into this and I don't seem to be able to reproduce this issue. I tried different auth flows (default and custom) with both default and custom storage solutions and there is no problem with maintaining the current session even after many reloads (or exiting the app and coming back). |
|
We did end up resolving this on our end. It had to do with how we were returning promises (or how/when they resolved) after replacing AsyncStorage with react-native-keychain. It has all been refactored since our initial fix, so unfortunately I don't have better details than that. |
|
We see this issue occasionally. It is rare but does happen. I have not been able to pin it down to a particular user action. They open their device and find themselves logged out. |
Exactly the same than @joebernard for us, except it is not that rare. We also removed the custom auth storage layer we had implemented, so we now use plain @Khairo-kh In another context we tried to call |
|
@samih7 I think this is an expected response if no identity pool is configured. Identity pools are used more on the authorization side of things. This can be something like authorizing authenticated users to access certain AWS services (S3 storage for example), or allowing unauthenticated access to certain resources. |


Describe the bug
We use
amplify-js3.3.20in our React Native app and some of our users are getting logged out from once very couple of days to several times a day.We retrieve user's token with the
currentSessionAPI to then redirect the user accordingly (to login page or home page), and it seems that this function sometimes throws a 'No current user' error even if the user was previously logged in. Before that, we were usingcurrentAuthenticatedUserinstead and the same problem was happening ('The user is not authenticated' error was thrown).We provide a custom storage using
react-native-sensitive-infoto store user data securely (we created a wrapper based on https://docs.amplify.aws/lib/auth/manageusers/q/platform/js#managing-security-tokens).To isolate the problem, we also released a version without
react-native-sensitive-infowhere we simply passed in anAsyncStorageimplementation as in https://docs.amplify.aws/lib/auth/manageusers/q/platform/js#managing-security-tokens but we still had the logout issue, which suggests it does not come from this other library.We also checked that ID & access tokens were correctly refreshed by
aws-amplifywhich seems to be the case.Resembles #4351.
To Reproduce
currentSessionthrows an error)Expected behavior
Stay logged in if we were previously logged in before.
What is Configured?
If applicable, please provide what is configured for Amplify CLI:
Environment
Smartphone (please complete the following information):
The text was updated successfully, but these errors were encountered: