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:
What I’ve Tried:
http://localhost:9099
.10.0.2.2
is used for the emulator host on Android.AndroidManifest.xml
.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?
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:
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.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.