0

I am trying to enable Firebase Google authentication in my React PWA, specifically the sigInWithPopup method. It works fine when I use the default URLs provided by Firebase (e.g., my-project.firebaseapp.com or my-project.web.app), but it's not working when I connect a custom domain. With this setup, I get a pop-up window that seems to be immediately handled by my own PWA rather than the Firebase auth server.

My custom domain is purchased from Cloudflare, and I am hosting my app through Firebase Hosting (not App Hosting). I've updated my app's Firebase config to use my custom domain. I've updated my Google Client ID to have my custom domain in the "Authorized JavaScript origins" and I've added https://my-custom-domain.app/__/auth/handler to the list of "Authorized redirect URIs". I've added my custom domain to the "Authorized Domains" section of my Firebase project as well. My authDomain Firebase config has also been updated to match my custom domain.

On the Cloudflare side, I've added the following config:

Name Type Content
A my-custom-domain.app 199.36.158.100
TXT my-custom-domain.app hosting-site=my-project
CNAME staging.my-custom-domain.app my-project-staging.web.app

My Firebase project says that the custom domain is successfully connected. I've spent the past few days troubleshooting this, even having an entire debug session with Gemini in Firebase. So far, no luck, but I think I've been able to narrow down areas I need to focus on.

Since the app works perfectly fine when I use the default Firebase URLs, I'm fairly confident the issue is either in my PWA config or my Cloudflare DNS config. I've tried tweaking the "rewrites" parameter in my firebase.json, but it still looks like the Firebase auth handler is not being called correctly. Instead, my PWA catches the request, and since I do not have any defined paths for the auth handler, it defaults to the app's Page Not Found message. Is there any extra handling I have to add in the app to account for this? I would assume no, since the app works with default Firebase URLs.

So, what else can I try to enable Firebase Google auth using my custom domain (and sub-domains)?

Other potentially useful info:

  • Here is a snippet of my React Router configuration if it helps:
const router = createBrowserRouter([
    {
        element: <Entrypoint />,
        errorElement: <PageNotFound />,
        children: [
            {
                path: '/',
                element: <Landing />
            },
            {
                path: 'settings',
                element: <Settings />
            }
        ]
    },
    {
        path: 'login',
        element: <Login />
    },
    {
        path: 'unauthorized',
        element: <Unauthorized />
    },
    {
        path: 'feedback',
        element: <Feedback />
    }
])

return ( <RouterProvider router={router} /> )
  • Here is the code used to call the Firebase auth:

const handleLogin = async () => {
    if ( !firebaseContext ) {
        console.error("No Firebase context provided")
        return
    }

    // Trigger Firebase login flow
    const user = await signInWithPopup(firebaseContext.auth, firebaseContext.authProvider)

    // Update session storage
    updateUserProfile(user, userContext.setProfile)

    // Navigate to main page         
    navigate("/")                                           
}

This is called when a user clicks a button:

<button onClick={handleLogin}>Log in</button>
  • I'm using React Context to store the Firebase app initialization:
export type FirebaseContextType = {
    authProvider:GoogleAuthProvider,
    app:FirebaseApp,
    analytics:Analytics,
    auth:Auth,
    db:Firestore
}

// Firebase setup
const firebaseApp = initializeApp(firebaseConfig)
const firebaseContext:FirebaseContextType = {
    authProvider: new GoogleAuthProvider(),
    app: firebaseApp,
    analytics: getAnalytics(firebaseApp),
    auth: getAuth(firebaseApp),
    db: getFirestore(firebaseApp)
}
  • I currently use env variables to set firebaseConfig. I've tried hardcoding this, but that didn't help

  • I have two Firebase projects, a prod and staging version of the same app, which are connected to my Cloudflare domain. Prod is linked to my-custom-domain.app and staging is linked to staging.my-custom-domain.app. I'm wondering if having this setup is potentially causing issues.

  • Updating the "rewrites" property in my firebase.json to make my app less aggressive in redirecting requests to index.html

  • Test setup using just one environment (staging) and not the production environment

  • Tried creating a new subdomain called auth.my-custom-domain.app and linking it to Firebase and adding it to the Google client ID auth

  • I'm not seeing any error messages in the console when I click the pop-up. Some time after closing the popup, I do get the error FirebaseError: Firebase: Error (auth/popup-closed-by-user)

2
  • Can you edit to include a complete minimal reproducible example of the relevant code handling the firebase auth/authentication? Commented Jun 9 at 16:07
  • @DrewReese I just updated my post. Hope that's enough Commented Jun 9 at 16:56

1 Answer 1

0

I found an alternative solution that does not directly answer my question but still provides the same behavior I needed. Perhaps this is also me not fully understanding how authorization and custom domains works earlier.

I came across this video from a few years ago that explicitly mentioned making a second site in your Firebase project and linking a custom auth domain to it. This is what I did:

  1. Inside my-project-staging Firebase project, I created a new site on the Hosting page and called it my-project-staging-auth. There is no code or custom app deployed to this project.
  2. I added a custom domain to my-project-staging-auth called auth.staging.my-custom-domain.app
  3. Added DNS config recommended by Firebase when going through the domain connection wizard.
  4. Added https://auth.staging.my-custom-domain/__/auth/handlers to my Google client ID
  5. Updated Firebase config on my app to use auth.staging.my-custom-domain for its authDomain property

This resolved the popup being handled by my main app. I'm still open to other alternatives if anyone has any, but this setup works well for my needs. Perhaps another way is to exclude certain routes in my React app so that Firebase handles them...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.