0

I'm trying to hook the Firebase Authentication emulator to my Flutter mobile project to perform some local testing. Unfortunately it seems it is not possible to do with the FlutterFire plugin.

There is no problem whatsoever to enable Firestore or Cloud Functions emulators, but I can not find a way for Authentication.

Is there someone with ideas or best practices to follow?

5 Answers 5

11

It looks like the method to connect to the Authentication emulator hasn't made it to the FlutterFire libraries yet.

An issue was logged for it in the Github repo, so you can follow its status there.

Update: It looks like this has now landed in version 0.20 (and later) of the firebase_auth package. The syntax should be something like:

FirebaseAuth.instance.useEmulator('http://localhost:9099');
5
  • Thanks Frank, I'll follow the issue and hope the feature will be available soon.
    – Garba
    Nov 13, 2020 at 3:42
  • Things have changed since such answer got accepted. Please see answer provided by Hettika, firebase auth emulator support has now been added to FlutterFire. Feb 27, 2021 at 15:37
  • Updated my answer with some links to the release. Feb 27, 2021 at 16:31
  • Make sure you are on the latest SDK versions too for flutter web development - <script src="https://www.gstatic.com/firebasejs/8.2.9/firebase-app.js"></script> in index.html or it won't load Mar 2, 2021 at 19:59
  • Well, at least v8.0.1 until github.com/FirebaseExtended/flutterfire/issues/4127 is fixed!! Mar 2, 2021 at 20:09
4

It is now available in the latest stable release (^0.20.1).

To use it, call FirebaseAuth.instance.useEmulator('http://localhost:9099')
It must be called prior to accessing any auth methods.

3
  • 1
    Until it gets properly documented in the FlutterFire documentation, apart from using firebase_auth ^0.20.1 + calling such 'useEmulator()' method, one key element missing here is to also slack your transport security rules in your android and/or ios configuration file(s), as highlighted in the link below, under the section "User notes". I personally was testing my flutter app on ios simulator, and kept getting a "Network Error" trying to connect to my local firebase auth emulator, until I followed those instructions. Now it's working. github.com/FirebaseExtended/flutterfire/pull/4263 Feb 27, 2021 at 15:34
  • franky duke - you are a saint. I've searched over and over for the reason I wasn't able to use this feature - only your comment saved me. Yes, If I read carefully that PR message - I could've found it. But it's a long thread. Thank you so much.
    – pinkasey
    Mar 4, 2021 at 15:43
  • Further to franky's comment - I fixed this network error (network-request-failed) I kept getting on iOS simulator by changing localhost:9099 to 127.0.0.1:9099 like so: await FirebaseAuth.instance.useEmulator('127.0.0.1:9099');
    – Marcel
    Jun 4, 2021 at 10:30
1

Configuring flutter app to work with firebase authentication in local emulators.

First:

Add one of the below lines before authentication function call see which one working for me loopback IP worked:

 firebaseAuth.useAuthEmulator('127.0.0.1',9099);
 //firebaseAuth.useAuthEmulator('http://localhost',9099);

Sample:

 Future<LocalUser?> signInAnonymously({required BuildContext context}) async {
    firebaseAuth.useAuthEmulator('127.0.0.1',9099);
    //firebaseAuth.useAuthEmulator('http://localhost',9099);

    UserCredential userCredential = await firebaseAuth.signInAnonymously();
    return LocalUser(user: userCredential.user);
  }

If you get network error do as follow.

Second-1: For Android.

Add android:usesCleartextTraffic="true" in AndroidManifest.xml path: android/app/src/main/AndroidManifest.xml.

 <application
    android:usesCleartextTraffic="true"      //<========
    android:name="io.flutter.app.FlutterApplication"
    android:label="firebaseauthexample"
    android:icon="@mipmap/ic_launcher">

Second-2: For IOS.

Add bellow code here ios/Runner/Info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
0

Firebase emulator has support to connect to web apps. *As a workaround, you can use the beta version of flutter to convert your mobile app to web app and test your it in your browser. You would have to add support for firebase services explicitly in index.html file. See: -

https://flutter.dev/web

https://flutter.dev/docs/get-started/web

0

"localhost" may be easier to use as follows:

Flutter has updated useEmulator with useAuthEmulator. To use local Authentication emulator, you can use useAuthEmulator method by passing Firebase emulator port and "localhost" keyword as follows:

await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);

Please be sure to pass this line before all auth methods as mentioned by @frank-van-puffelen before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.