Skip to content

fix(auth): prevent null hydration on pagehide/reload#10123

Open
gorthitk wants to merge 3 commits into
firebase:mainfrom
gorthitk:Fix-IndexedDB
Open

fix(auth): prevent null hydration on pagehide/reload#10123
gorthitk wants to merge 3 commits into
firebase:mainfrom
gorthitk:Fix-IndexedDB

Conversation

@gorthitk

@gorthitk gorthitk commented Jun 29, 2026

Copy link
Copy Markdown

Description

This PR addresses an issue where Firebase Auth unexpectedly hydrates to a null state (signing the user out) on page reload or when returning to a backgrounded/restored tab.

The Problem

During pagehide (e.g., page unload or reload), the browser can interrupt/abort active IndexedDB transactions. In WebKit/Safari, instead of throwing a catchable error when a transaction is aborted during unload, the getAll() request on the object store resolves with an empty array ([]).
Because [] is truthy, the SDK's polling loop (_poll()) processes it, compares it to the in-memory cache, assumes the auth keys were deleted in another tab, and propagates a null Auth state.

The Fix

  1. Added pagehide and pageshow event listeners to IndexedDBLocalPersistence to set an isHiding flag, stop the polling timer, and close the IndexedDB connection during page transitions.
  2. Guarded _openDb(), _withRetries(), and _poll() using the isHiding flag to prevent new database transactions from starting during pagehide, and discard any in-flight results.
  3. Wrapped the polling body in a try/catch block to prevent unhandled promise rejections if the database transaction is aborted.

Closes #10041

@gorthitk gorthitk requested review from a team, pashanka and sam-gc as code owners June 29, 2026 17:32
@changeset-bot

changeset-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 020b30c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@firebase/auth Patch
@firebase/auth-compat Patch
firebase Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces lifecycle event handling for pagehide and pageshow in IndexedDBLocalPersistence to gracefully close the database and stop polling when the page is hidden, and resume polling when the page is shown. Feedback was provided to optimize the pageshow event listener by adding an if (this.isHiding) guard to prevent redundant polling timer restarts on initial page load.

Comment thread packages/auth/src/platform_browser/persistence/indexed_db.ts Outdated

@macastelaz macastelaz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this fix together!

Comment thread packages/auth/src/platform_browser/persistence/indexed_db.ts Outdated
Comment thread packages/auth/src/platform_browser/persistence/indexed_db.ts Outdated
Comment thread packages/auth/src/platform_browser/persistence/indexed_db.ts Outdated
Comment thread packages/auth/src/platform_browser/persistence/indexed_db.ts

@hsubox76 hsubox76 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is a bug fix? Can you add a changeset? yarn changeset and use "patch".

@gorthitk gorthitk requested review from a team as code owners July 11, 2026 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auth hydrates null after IndexedDB Auth record is present before pagehide but missing on next reload

3 participants