Skip to content

Firebase onAuthStateChanged hangs on startup #8478

@GaurangTandon

Description

@GaurangTandon

Operating System

Windows

Environment (if applicable)

Chrome 128

Firebase SDK Version

10.11.0

Firebase SDK Product(s)

Auth

Project Tooling

MV3 Chrome extension service worker built using webpack

Detailed Problem Description

Problem

onAuthStateChanged() hangs on extension service worker startup and then remains hanged until the extension service worker is restarted (either by restarting the browser or by manually reloading the extension). Note that we are using indexeddb persistence so we expect the Firebase instance to get logged in automatically whenever the service worker restarts.

This issue happens more often when the user logs into computer in the morning of a workday. In that case, the user's Firebase ID token from yesterday has expired. Based on that, and based on the logs given below, we suspect the hang is when Firebase is regenerating the ID token. We are not sure why that is, maybe due to network conditions? Our hypothesis could be wrong though.

Logging details

We added logging into the underlying @firebase/auth package using npx patch-package, and got the hang on these two cases:

Case 1:

tbLog('IWP: creating'); // <-- this line got printed
this.persistenceManager = await PersistenceUserManager.create(this, persistenceHierarchy); // <-- hanged here
tbLog('IWP: created'); // <-- this line never got printed

Case 2:

/// in _initializeWithPersistence
tbLog('IWP: iniatilizing current user'); // <-- this line got printed
await this.initializeCurrentUser(popupRedirectResolver);
tbLog('IWP: finished current user'); // <-- this line never got printed

/// in initializeCurrentUser
if (futureCurrentUser) {
  tbLog('ICU: c1'); // <-- this line got printed
  return this.reloadAndSetCurrentUserOrClear(futureCurrentUser); // <-- hanged here
}

Questions:

Is there an easy way - for our code outside the Firebase package - to restart the Firebase app initialization in case it hangs like this? For example: is there a restartApp equivalent to initializeApp. We would wait for a fix in the official package, but we really need a temporary workaround as this issue is severely impacting the user experience

PS: Let us know if you need additional logging inside the Firebase Auth package, and what exactly. It can take us a few days to get the data as this happens rarely and only for some users. So we would still need the workaround.

Steps and code to reproduce issue

We have roughly the following setup:

import { getAuth, onAuthStateChanged, } from 'firebase/auth/web-extension';

const firebaseApp = initializeApp(firebaseConfig);
const firestore = initializeFirestore(firebaseApp, {
  experimentalForceLongPolling: true
});
onAuthStateChanged(getAuth(firebaseApp), (user) => {
  console.log(user);
  if (user) {
    enableIndexedDbPersistence(firestore, { forceOwnership: true });
  }
}});

Note that we are using the web-extension entrypoint into firebase/auth. Note that we initialize persistence after auth has enabled. I'm not sure if it should be the other way around, I couldn't find any official docs around it, or if that matters at all.

I have attached the patch file generated using patch-package.
@firebase+auth+1.7.1.patch

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