Skip to content

fix(auth,web): fail onWaitInitState instead of hanging when the first auth state is an error - #18705

Open
chiliec wants to merge 1 commit into
firebase:mainfrom
chiliec:fix/auth-web-wait-init-state-error
Open

chiliec wants to merge 1 commit into
firebase:mainfrom
chiliec:fix/auth-web-wait-init-state-error

Conversation

@chiliec

@chiliec chiliec commented Sep 22, 2026

Copy link
Copy Markdown

Description

On Flutter web, Firebase.initializeApp() could hang forever (blank page until a manual reload) when the JS SDK's first onAuthStateChanged callback fires with an error — e.g. an IndexedDBLocalPersistence._openDb rejection during initial user restoration.

Auth.onWaitInitState() (awaited from FirebaseAuthWeb.registerWithensurePluginInitialized) had two defects on that path:

  1. The error callback never completed the Completer, so await completer.future waited forever.
  2. It dereferenced _changeController!, which is still null during plugin registration (nothing has subscribed to onAuthStateChanged yet), so the JS callback threw a null-check error and the original error was swallowed.

This PR:

  • completes the completer with the error (guarded by isCompleted), so onWaitInitState() rejects instead of hanging;
  • forwards the error to _changeController only when it exists (?.);
  • unsubscribes the JS listener in a finally, on both the success and error paths;
  • wraps the onWaitInitState() call in registerWith with the existing guardAuthExceptions, so Firebase.initializeApp() surfaces a regular FirebaseAuthException (FirebaseErrorcode/message conversion) instead of a raw JS object.

The happy path is unchanged: _initUser is still captured from the first next callback and onWaitInitState() still completes normally.

Related Issues

Fixes #18703

Tests

New packages/firebase_auth/firebase_auth_web/test/on_wait_init_state_test.dart (browser test) drives Auth.onWaitInitState() against a minimal JS stub of onAuthStateChanged:

  • first auth state arrives → completes and unsubscribes;
  • first auth state is an error → rejects with that error (with a 2 s timeout guard) and unsubscribes.

Run locally with flutter test --platform chrome (Flutter 3.47.2, chrome-headless-shell 153):

  • on main the error case failsTypeError: ... 'TypeErrorImpl' is not a subtype of type 'JSObject' from the _changeController! null check, and without that the future would never complete;
  • with this change: on_wait_init_state_test.dart +2: All tests passed!, get_firebase_auth_exception_test.dart +2, auth_emulator_test.dart +11.

dart analyze on the package: No issues found!; dart format clean.

Not run here: the e2e example app / a real browser session with IndexedDB blocked.

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

… auth state is an error

When the JS SDK's first onAuthStateChanged callback fires with an error
(e.g. IndexedDB _openDb rejection at page load), onWaitInitState never
completed its completer and dereferenced a null _changeController, so
Firebase.initializeApp() hung forever and the app stayed on a blank page.

Complete the completer with the error, unsubscribe in either case, and
surface it from registerWith as a FirebaseAuthException via
guardAuthExceptions.

Fixes firebase#18703
@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@google-cla

google-cla Bot commented Sep 22, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@dganzella

Copy link
Copy Markdown

I approve this, good stuff

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants