I need to check if the user is fresh or already logged in without delaying opening of my app and based on the login state of user, screens should be shown
Asked
Active
Viewed 1,210 times
3
-
1Possible duplicate of [Firebase Auth state check in Flutter](https://stackoverflow.com/questions/49811909/firebase-auth-state-check-in-flutter) – shadowsheep Jan 21 '19 at 17:01
-
I don't think it's possible to do it without delaying with firebase alone. Maybe one way would be to save the user externally to Firebase, like in cache or something, and just assume that everything is fine on the backend. After that, you can do a second check/update to get the user again if you need to make sure the user is still the same, active, and everything is correct. Apart from that, the duplicate pointed above seems to work nicely. If you really need without that delay, maybe update your question and title to clarify it. – George Jan 21 '19 at 18:18
1 Answers
1
There will always be a delay checking user auth status from Firebase Auth. For example, listening for auth state changes using FirebaseAuth.instance.authStateChanges().listen()
FirebaseAuth.instance
.authStateChanges()
.listen((User? user) {
if (user == null) {
print('User is currently signed out!');
} else {
print('User is signed in!');
}
});
As a workaround, what I did on my end was to store auth status using shared_preferences. Using this, the app can instantly redirect the user to the home page if an auth session was stored. FirebaseAuth authStateChanges listener can just update the SharedPreferences later on if there's any change on user auth status.
Omatt
- 8,564
- 2
- 42
- 144