Search code examples
androidflutterfirebasefirebase-authenticationfirebase-tools

Firebase Authentication Emulator in Flutter on Android: `Logging in as [email protected] with empty reCAPTCHA token`


I’m trying to use the Firebase Authentication Emulator with Flutter on Android, but I’m encountering an issue during signInWithEmailAndPassword. It logs the following message:

FirebaseAuth: Logging in as [email protected] with empty reCAPTCHA token

The process gets stuck for a while and eventually throws the following exception:

Unexpected FirebaseAuthException: [firebase_auth/network-request-failed] A network error (such as timeout, interrupted connection or unreachable host) has occurred.

In my main.dart, I initialize Firebase and configure the Authentication Emulator if useEmulator is set to true:

await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);
if (useEmulator) {
  String host = Platform.isAndroid ? '10.0.2.2' : 'localhost';
  await FirebaseAuth.instance.useAuthEmulator(host, 9099);
}

I then attempt to sign in as follows:

try {
  await FirebaseAuth.instance.signInWithEmailAndPassword(
    email: email,
    password: password,
  );
} on FirebaseAuthException catch (e) {
  if (e.code == 'invalid-credential') {
    if (kDebugMode) {
          print('Invalid credentials');
        }
      } else {
        if (kDebugMode) {
          print('Unexpected FirebaseAuthException: $e');
        }
      }
    } catch (e) {
   if (kDebugMode) {
    print('Unexpected Exception: $e');
  }
}

Flutter Doctor:

  • Flutter version: 3.27.1 (stable)
  • Android toolchain: Android SDK version 35.0.0
  • macOS version: 15.2 (24C101 darwin-arm64)
  • Xcode version: 16.0
  • Android Studio version: 2024.2
  • VS Code version: 1.96.4
  • Connected devices: 5
  • Network resources: Accessible

What I’ve Tried:

  1. Verified that the Authentication Emulator is running and accessible at http://localhost:9099.
  2. Ensured 10.0.2.2 is used for the emulator host on Android.
  3. Confirmed cleartext traffic is enabled in AndroidManifest.xml.
  4. Tested the same code against production Firebase Auth, which works without issues.

Why does the Firebase Authentication Emulator fail with Logging in as [email protected] with empty reCAPTCHA token? Is there something specific to the Authentication Emulator or its setup that I might be missing?


Solution

  • I found the problem. May have been obvious but did not realise at the time.

    This issue, along with a timeout error I was also experiencing, occurred because I was trying to run the app on a physical device in debug mode while the Firebase Emulator was running on my Mac. I assumed that my Mac's network was automatically accessible to my physical device (as it is in debug mode), but that’s not the case. This behaviour only works automatically when using the Android emulator, where 10.0.2.2 maps to the host machine’s localhost.

    To fix this, I had to:

    1. Change the Firebase Emulator settings to bind to 0.0.0.0 instead of localhost. This allows the emulator to listen on all available network interfaces, making it accessible from devices on the same network.
    2. Use my Mac’s local network IP address (e.g., 192.168.x.x) in the app code when connecting to the emulator:
      await FirebaseAuth.instance.useAuthEmulator('192.168.x.x', 9099);
      

    With these changes, the physical device was able to communicate with the Firebase Emulator, and everything worked as expected.