Firebase sign-in fails with 'missing initial state' after a redirect
Unable 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
The 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
authDomain was changed to your own domain, but /__/auth/handler returns 404The app is not on Firebase Hosting and nothing serves /__/auth/Fix - 3
You call signInWithRedirect only because it was the first example you found.The flow does not need a redirect at allFix
- 1
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 flowConfirmed by firebase.google.com
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/handler2Fix
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/handlerConfirmed by firebase.google.com
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 SafariConfirmed by firebase.google.com
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:
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Web apps
Attempted import error: firebase/app has no default export
Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').
Since the modular SDK (version 9), firebase/app no longer exports one firebase object.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb 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 fixWeb apps
SyntaxError in firebase-messaging-sw.js: import statement outside a module
Uncaught SyntaxError: Cannot use import statement outside a module (at firebase-messaging-sw.js:1:1)
A service worker is a classic script, so an import line in firebase-messaging-sw.js is a syntax error.
2 causes, each with a fixWeb 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 fixWeb apps
Firebase Messaging cannot register its default service worker
messaging/failed-service-worker-registration
Firebase Messaging registers a file called firebase-messaging-sw.js from the root of your domain.
3 causes, each with a fix