I have an App in, live in the AppStore developed in Swift where I use Firebase Auth
. Everything is working fine. In the next update the app will be a Flutter App, but also using the same Firebase Backend, including Auth.
When I test the app, the authState
is working fine. The user is still logged in after an app restart.
HOWEVER when I got the old app, update it via TestFlight to the new version, the user always gets logged out after an app restart. The only fix I found is deleting the app (new version) and installing it again.
Is this a known issue? Is there something I can do? This is super frustrating for old users of my app, who update to the new version.
This is how I keep track of the authState
:
static bool isLoggedIn() {
if (_isLoggedIn == null) {
_firebaseAuth.authStateChanges().listen((User? user) {
_isLoggedIn = user?.uid != null;
});
_isLoggedIn = _firebaseAuth.currentUser?.uid != null;
}
return _isLoggedIn ?? false;
}
Also, I got this method inside my main
, so a new user will be logged out the first time he opens the new app:
static Future<void> signOutUserIfUserDeinstalledAndReinstalledApp() async {
bool? isFirstStart = await LocalStorageService.getData(
key: LocalStorageKeys.isFirstStart,
);
if (isFirstStart == null && !kIsWeb) {
// await PushNotificationService.cancelAllNotifications();
await LocalStorageService.clear();
await signOut(deleteMessaingToken: false);
await LocalStorageService.setBool(
key: LocalStorageKeys.isFirstStart,
value: false,
);
}
}
2
Answers
The fact of whether a user is authenticated or not (user == null or not) is only known to (determined by) the local device (not the backend) by means of a stored session token.
There must be a difference (between Flutter and ios Firebase auth implementation) in where and how this token is stored.
I dont see how this could be solved.
Also dont see why it should be a huge inconvenience —- I am unexpectedly logged out of apps all the time and I just shrug and log back in. Its only going to happen once.
You can achieve it using the method channel. It’s bit tricky but it’s possible .
So we can create a method channel to check on the platform(on iOS) to check for the user details, auth status and session. I think you are pretty good with swift/objective c so it won’t be an issue.
I am not good with swift, just creating a structure so you can get the idea. Following code is for battery but you can do the same for FirebaseAuth from swift side.
On Flutter side
this answer could be improved this is just a reference way. any help will be appreciated.
ref: https://docs.flutter.dev/platform-integration/platform-channels
for continuous listen(stream): https://github.com/flutter/flutter/tree/master/examples/platform_channel_swift