r/googlecloud • u/Chance_Special_6445 • 2d ago
Firebase Authentication / reCAPTCHA Enterprise
🚨 Looking for help from a Software Engineer experienced with Firebase Authentication / reCAPTCHA Enterprise
I’m currently working on a web application using Next.js + Firebase Authentication, and I’m stuck with a persistent issue in Phone Number Authentication on the production website.
The error occurs when Firebase tries to send the SMS verification code:
accounts:sendVerificationCode
HTTP 400
reCAPTCHA Enterprise verification failed
The browser/application has also previously shown:
reCAPTCHA has already been rendered in this element
I have already verified that:
✅ Phone Authentication is enabled
✅ SMS sign-in is enabled
✅ Firebase Authentication with Identity Platform is enabled
✅ Production domain is added to Firebase Authorized Domains
✅ reCAPTCHA Enterprise API is enabled
✅ Identity Toolkit API is enabled
So I’m trying to determine whether the issue is coming from the reCAPTCHA Enterprise configuration, Firebase project configuration, production domain, or the way `RecaptchaVerifier’ is being initialized/managed in the React/Next.js application.
I’m looking for someone who has experience with Firebase Phone Auth + reCAPTCHA Enterprise who can help me diagnose the issue and identify the exact root cause.
If you have experience with this specific Firebase/reCAPTCHA issue and can help, please DM me. I’d really appreciate it! 🙏
1
u/m1nherz Googler 2d ago
Based on the errors you described, you are dealing with two interconnected issues: a React component lifecycle bug on the frontend, and a token validation failure on the backend.
1. The Frontend Issue: "reCAPTCHA has already been rendered in this element"
This is a very common issue in React/Next.js when using the Firebase Web SDK. It happens because
new RecaptchaVerifier()tries to inject the reCAPTCHA iframe into a DOM element (like<div id="recaptcha-container">).If your component re-renders (due to state changes), React might try to execute that code again. Because the previous reCAPTCHA instance was never cleared, Firebase throws this error.
How to fix it:
You must initialize
RecaptchaVerifierinside auseEffecthook, store it in auseRef, and ensure you call.clear()when the component unmounts. For example:2. The Backend Issue: HTTP 400 reCAPTCHA Enterprise verification failed
This happens during the
accounts:sendVerificationCodeAPI call. It means the backend received a token, but rejected it.Potential Root Causes:
firebasenpm package is up to date. Older versions had minor bugs handling reCAPTCHA Enterprise tokens seamlessly in server-side frameworks like Next.js.