Auth persistence sometimes clears the user (original) (raw)

[REQUIRED] Describe your environment

[REQUIRED] Describe the problem

Steps to reproduce:

Users have been reporting to me that they are being signed out of my website after a few days. This has only started happening since I updated my app from Firebase v8 to the v9 modular SDK. My app only uses Custom Authentication.

I initialize Auth as follows:

const authOptions = { persistence: [ indexedDBLocalPersistence, browserLocalPersistence, browserSessionPersistence ] };

const auth = initializeAuth(app, authOptions);

In my main app initialization code I attach a global Auth listener:

onAuthStateChanged(auth(), user => { console.log(onAuthStateChanged(${user ? user.uid : null})); if (!user) { // ... } });

Sometimes I seem to get onAuthStateChanged called with a null user even after the user has been signed in for days. For my own app logic I also store my own user state in LocalStorage and this state persists infinitely, whereas Firebase seems to clear sometimes.

So I get errors from the following situation:

  1. App loads
  2. Synchronously check my own LocalStorage user store, find a non-null user
  3. Allow the user to progress deeper into the app, assuming that they are signed in
  4. Asynchronously receive an onAuthStateChanged callback from Firebase with user === null
  5. Throw an error and punt the user back to the sign-in screen