-
Notifications
You must be signed in to change notification settings - Fork 962
Closed
Description
[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:
- App loads
- Synchronously check my own LocalStorage user store, find a non-null user
- Allow the user to progress deeper into the app, assuming that they are signed in
- Asynchronously receive an onAuthStateChanged callback from Firebase with
user === null
- Throw an error and punt the user back to the sign-in screen