Skip to content

Auth persistence sometimes clears the user #5874

@hatboysam

Description

@hatboysam

[REQUIRED] Describe your environment

  • Operating System version: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0
  • Browser version: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0
  • Firebase SDK version: 9.6.1
  • Firebase Product: auth

[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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions