Firebase auth/unauthorized-domain Error After Deploy: Fix
Google or GitHub sign-in works on localhost but fails on your live site with auth/unauthorized-domain? Add the exact domain, handle preview URLs, and fix redirect sign-in on custom domains.
The Firebase error auth/unauthorized-domain means the domain your site runs on is not in the Authorized domains list of your Firebase project. Open Authentication, Settings, Authorized domains in the Firebase console, add the exact host shown in the error, and try again. If you use redirect sign-in on a custom domain, you may also need to set authDomain to that domain.
It shows up right after the first deploy, after a domain change, or on a preview URL. Localhost is allowed by default, so everything works on your laptop. Here is how to fix it in order.
Step 1: Read the Error Code and the Domain
Catch the error and log its code, so you do not guess:
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
try {
await signInWithPopup(getAuth(), new GoogleAuthProvider());
} catch (err) {
console.error(err.code, err.message, window.location.host);
}
If the code is auth/unauthorized-domain, the host in window.location.host is the one to add. Other codes mean other problems, such as auth/popup-blocked or auth/popup-closed-by-user. Write-ups on this error in a deployed site and a step by step Google login fix both come down to the same missing entry.
Step 2: Add the Domain to Authorized Domains
In the Firebase console, go to Authentication, then Settings, then Authorized domains, and click Add domain.
- Use the host only.
app.example.com, nothttps://app.example.com/login. - Add each host you use.
example.comandwww.example.comare different entries. Subdomains are not covered automatically. - Match the project. Check that your app's config (
apiKey,projectId) points to the same Firebase project where you added the domain. A second project for staging is a common mix-up. - Wait a minute and hard refresh. Changes can take a short time to apply.
One report notes the error can continue after adding domains until the exact full host from the error is added, so copy it from the message.
Step 3: Handle Preview and Builder URLs
Hosts that change per deploy will fail one by one: Vercel or Netlify preview URLs, Replit or Lovable published URLs, and similar. Each unique host must be authorized. Options:
- Add the stable production domain and test sign-in only there.
- Use a fixed staging domain for previews instead of random ones.
- For quick tests, use the Firebase Auth emulator and keep real providers for production.
A thread about an AI app builder with Firebase Authentication shows the same error when the builder's host was never added. If you build with an AI tool, check which URL your app is really served from.
Step 4: Check Provider Settings Too
Adding the domain to Firebase does not change the sign-in provider. For Google, GitHub or Facebook, the provider has its own list of allowed redirect addresses. Firebase uses a handler path on your auth domain:
https://<your-authDomain>/__/auth/handler
If you see redirect_uri_mismatch instead, follow Google OAuth redirect_uri_mismatch in Production. Firebase's own domain (<project>.firebaseapp.com) is authorized by default, so a mismatch usually appears only after you change authDomain or move providers.
Step 5: Fix Redirect Sign-In on a Custom Domain
signInWithPopup usually works once the domain is authorized. signInWithRedirect can still fail on browsers that block third-party storage, because your app and the auth handler sit on different domains. Firebase has best practices for signInWithRedirect on browsers that block third-party storage access. The options listed are:
- Use
signInWithPopupinstead of redirect, if the popup experience is fine for your users. - Set
authDomainto your own domain if you host on Firebase Hosting with that custom domain:
const firebaseConfig = {
apiKey: "...",
authDomain: "app.example.com", // your custom domain, not project.firebaseapp.com
projectId: "...",
};
- Proxy
/__/auth/to Firebase if you host elsewhere, so the handler is served from your domain. Requests tohttps://app.example.com/__/auth/*must reach<project>.firebaseapp.com/__/auth/*.
After you change authDomain, update the provider's redirect URI to https://app.example.com/__/auth/handler. Hosting with Firebase App Hosting and a custom domain has had reports of /__/auth/ paths returning 404 in this Firebase community thread, so open that URL in a browser and confirm it returns a page, not a 404.
Step 6: Keep Environments Separate
Many teams hit this error because dev and production share config. Keep one config per environment:
const config = {
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
};
Set these in your hosting dashboard for production, then redeploy. Build-time variables are baked in at build, so changing them without a new build changes nothing. After any domain change, add a step to your launch checklist: sign in once on the live site with a fresh browser profile.
Step 7: Know When Sign-In Breaks
This failure hits every new visitor, but it does not crash your server. Your backend logs stay quiet, and an uptime ping on the home page still returns 200. See Uptime Monitor Says Up but Users See Errors for why that happens. You need to see browser errors, or be told by a user. If your Cloud Functions also fail, check Firebase Function PERMISSION_DENIED on Firestore.
How Relia Helps
Relia supports Firebase apps and captures errors from the browser, so a spike of auth/unauthorized-domain on a new domain shows up as one incident with the page, browser and number of users affected, explained in plain words. Uptime monitors and checks of key pages test your live site from outside. Setup for browser apps is npx relia-sdk@latest init. There is a live demo you can open first.
FAQ
Why does Firebase sign-in work on localhost but not in production?
localhost is authorized by default. Your production domain is not until you add it under Authentication, Settings, Authorized domains.
Do I need to add www and non-www separately?
Yes. Each host is its own entry, and subdomains are not covered by a parent domain.
Should I add the domain to Google Cloud as well?
Adding it in Firebase fixes auth/unauthorized-domain. If the provider then shows a redirect error, add https://<authDomain>/__/auth/handler to the provider's allowed redirect URIs.
Why does sign-in fail only on some browsers after I added the domain?
Redirect sign-in can fail where third-party storage is blocked. Switch to popup sign-in, use your own authDomain, or proxy /__/auth/.
Can I use a wildcard domain?
Do not count on it. Add each real host you use, and keep preview URLs for a fixed staging domain.