Best way to keep user logged-in on React Native?

Hello!

So I’ve been using the @meteorrn/core library on a production app; it’s a great library and a good translation of Meteor on a RN environment. But something that users notice me constantly is that after they exit the app (not closing it) and reopen it, they can’t call the subscriptions nor the methods of the server.

After some logging I found out that the app does connect to the server and it still keeps the token and the user, but the server logs out the user between that time frame so there’s this conflict of the client having the user logged in and in the server is not.

I’ve done various solutions like calling directly Meteor._loginWithToken, ditch the autoReconnect flag and use a custom reconnection process with useEffect, etc. But none of them helped me to fix this issue so I think that I’m not using a correct process or ignoring something important.

Have any of you encountered with something like this? And if so, what did you do to fix it? I feel like I’m still new in the Meteor environment so I’d like to hear your opinions and experience

While writing this I just found out this post that had the exact problem that I have but with facebook tokens and the first major version of react-native-meteor in 2016. It’s not the same codebase on any of the libraries and it’s not the same log in method but I add it to give a little bit of context respect the ‘server logging out the user’ part User logged in on client, but not on server (React Native)

The React Native package doesn’t have a large amount of support and I’ve gotten into just changing the package to make it work better for me.
Personally I’ve been using this commit in my package.json and it has generally been good:

@meteorrn/core”: “github:ToyboxZach/meteor-react-native#3160e269951444cd16e97d6e8b6dd3bf76d15826”

But note, I use only a subset of the features of Meteor, so I’m not making any promises about everything working. This is definitely an issue with the react-native package itself and not Meteor

1 Like

Thank you! I’ll be definitely checking that repo to see your work :grin:

hey @schlaegerz I updated the package a lot, we are currently at version 2.8.1

@jesusej I have some example code on how we handle the logins here:

maybe it can help you

3 Likes

Oh, that’s great! I’ll be also checking out your code. Thank you so much for your response.

It sounds like your app’s session isn’t syncing properly when reopened. The issue might be with token expiration after the app is closed, so you may need to refresh or reauthenticate when reopening. Double-check your reconnection logic and ensure you’re calling Meteor.reconnect() or forcing a re-login after reopening the app. Also, verify that the token is stored securely, and try using Meteor._loginWithToken() to restore the session once the app reconnects. Hopefully, this helps resolve the issue! Let me know if you need more details.