firebase / firebase/firebase-js-sdk

onAuthStateChanged hangs on startup at initializing current user step

Open
#8,481 5 comments 1 reaction 0 assignees View on GitHub
api: auth needs-attention question
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### 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 hang is slightly more frequent compared to the other one in PersistenceManager. We suspect the 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 this case:

```javascript
/// 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
}
```

**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 though.

### Steps and code to reproduce issue

We have roughly the following setup:

```javascript
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](https://github.com/user-attachments/files/16868964/%40firebase%2Bauth%2B1.7.1.patch)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.