Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Firebase sign-in fails with 'missing initial state' after a redirect

ErrorUnable to process request due to missing initial state.

Short answer

The sign-in page is opened on one domain and the result is read back from another. Browsers that partition or block third-party storage lose the saved state in between. Serve the auth helper from your own domain (authDomain or a proxy for /__/auth/), or use signInWithPopup instead of the redirect.

  • Applies to Firebase Authentication web SDK, FlutterFire firebase_auth (web)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which authDomain the app is initialised with. If it is the default project.firebaseapp.com while the site runs on another domain, the redirect flow is cross-origin.

    // web/index.html or your Firebase options
    // look for: authDomain: "your-project.firebaseapp.com"
    grep -rn "authDomain" web/ lib/
  2. 2Match what you see, then jump to its fix

    1. 1The error appears in Firefox, Safari or current Chrome, but not in a browser that still allows third-party storage.authDomain is not the domain that serves the appFix
    2. 2authDomain was changed to your own domain, but /__/auth/handler returns 404The app is not on Firebase Hosting and nothing serves /__/auth/Fix
    3. 3You call signInWithRedirect only because it was the first example you found.The flow does not need a redirect at allFix

What does the error look like?

  • Unable to process request due to missing initial state.
  • This may happen if browser sessionStorage is inaccessible or accidentally cleared.
  • Using signInWithRedirect in a storage-partitioned browser environment.

Cause 1 of 3

authDomain is not the domain that serves the app

1Check

Redirect sign-in uses an iframe that talks to your Firebase Hosting domain. Browsers that block third-party storage access break that connection unless the app is served from the same domain. Apps hosted on a firebaseapp.com subdomain are not affected.

grep -rn "authDomain" web/ lib/

2Fix

Set authDomain to the domain that serves your app, add https://YOUR-APP-DOMAIN/__/auth/handler to the redirect URIs of your OAuth provider (and the ACS URL for SAML), make sure the domain is in the Firebase authorized domains list, and redeploy so /__/firebase/init.json is current.

// Firebase config: authDomain is the domain that serves the app
authDomain: "app.example.com"

3Verify

Sign in again in the browser that failed.

# open https://app.example.com and start the sign-in flow

Confirmed by firebase.google.com

Did this fix it?

Cause 2 of 3

The app is not on Firebase Hosting and nothing serves /__/auth/

1Check

With another host, the paths under /__/auth/ must reach your project's firebaseapp.com host. The forwarding has to be transparent, not a 302 redirect.

curl -I https://app.example.com/__/auth/handler

2Fix

Add a reverse proxy rule that forwards /__/auth to the project's firebaseapp.com domain, then finish the authDomain and redirect URI steps from the first cause.

location /__/auth {
  proxy_pass https://YOUR-PROJECT.firebaseapp.com;
}

3Verify

The handler URL answers on your own domain.

curl -I https://app.example.com/__/auth/handler

Confirmed by firebase.google.com

Did this fix it?

Cause 3 of 3

The flow does not need a redirect at all

1Check

Popup sign-in does not depend on the cross-origin iframe, so it is not affected by the storage blocking. Popups can be blocked by the browser and are less smooth on mobile.

grep -rn "signInWithRedirect\|getRedirectResult" lib/ web/

2Fix

On Flutter web call signInWithPopup, and keep signInWithProvider for Android and iOS. You no longer need to read a redirect result.

if (kIsWeb) {
  await FirebaseAuth.instance.signInWithPopup(provider);
} else {
  await FirebaseAuth.instance.signInWithProvider(provider);
}

3Verify

Sign in from the browser that failed. The popup opens and the user is signed in.

# start the sign-in flow in Firefox or Safari

Confirmed by firebase.google.com

Did this fix it?

How do I stop it coming back?

  • Test sign-in in Firefox and Safari as well as Chrome before a release.
  • Keep authDomain on the domain that serves the app and keep its redirect URIs in step with it.
  • Prefer the popup flow on web unless you have a reason to use the redirect.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Google sign-in popup blocked by Cross-Origin-Opener-Policy

The page loses its reference to the sign-in popup window, because the opener policy severs it

If your site sends Cross-Origin-Opener-Policy: same-origin, a sign-in popup from another origin lands in a separate browsing context group, so your page cannot reach it.

3 causes, each with a fix

Web apps

Browser blocks a Firebase Storage request at the CORS preflight

Response to preflight request doesn't pass access control check: It does not have HTTP ok status

A browser sends an OPTIONS preflight before some cross-origin requests, and Cloud Storage answers it only from the CORS configuration on the bucket.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.