1

I just saw a tutorial, where the author although was using shared-ref to store the user name, email etc locally, but he didn't use anything to store currentUser on a flutter app, which was connected to firebase. and hence in splash screen, he used if currentUser != null, then goto Home Screen, and it worked fine on restarting the app. hence to log out, again he used firebaseAuth.signOut() . so, does firebase stores currentUser automatically on the local storage of the app?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Riyazat Durrani
  • 588
  • 1
  • 8
  • 20

2 Answers2

2

Firebase Authentication SDKs store the credentials of the user in local storage, and then restore the user from there when the app restarts/page reloads.

Note that this doesn't always means that currentUser != null will work on app startup/page load, as restoring the credentials requires an asynchronous call to the server and currentUser will be null until that call completes. For the best results listen/respond to authStateChanges as shown in the documentation on authentication state.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • How do I differentiate currentUser being `null` due to the user being signed out, vs due to the asynchronous call taking a while? For the first case, you’d show a login page, but for the latter you’d just display a loading screen… right? – Nathan Tew Sep 18 '22 at 03:05
  • That's precisely why you'll want to listen to the auth state. The listener won't fire until the auth state has been restored or failed to restore, so when you get an initial value you can safely navigate. If you want to show something before that check, you'll have to store some state yourself as Michael Bleigh showed in this I/O talk: https://www.youtube.com/watch?v=NwY6jkohseg&t=1311s – Frank van Puffelen Sep 18 '22 at 03:47
1

I don’t think it’s stored locally in terms of shared preferences/ user defaults. I had an issue where without logging out, but uninstalling and reinstalling the app resulted in the app already knowing who I was. Turns out, it was stored in keychain (on iOS, I don’t know the android counterpart). See this answer for more detail on my particular understanding of how firebase saves the user:

https://stackoverflow.com/a/69621552/12132021

Jared Anderton
  • 826
  • 9
  • 12
  • I guess, the issue you are talking about is related to caching, turns out that indeed its stored locally, as stated by the accepted answer here. https://stackoverflow.com/questions/64042555/firestore-flutter-save-current-user-data-to-device-locally – Riyazat Durrani Jan 09 '22 at 19:27