Skip to content

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

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

fix(auth): prevent null hydration on pagehide/reload#10123
gorthitk wants to merge 2 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

⚠️ No Changeset found

Latest commit: 2a91ddb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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
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

2 participants