Skip to content

Commit 84a179b

Browse files
Salakarmikehardy
andcommitted
feat(firebase_auth): implement support for useEmulator (#4263)
Co-authored-by: Mike Hardy <[email protected]> Co-authored-by: Mike Diarmid <[email protected]>
1 parent 4e96d64 commit 84a179b

File tree

23 files changed

+209
-50
lines changed

23 files changed

+209
-50
lines changed

.github/workflows/firebase_auth.yaml

+15-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
branches:
1010
- master
1111
paths-ignore:
12-
- "docs/**"
12+
- "docs/**"
1313

1414
env:
1515
FLUTTERFIRE_PLUGIN_SCOPE: "*firebase_auth*"
@@ -27,9 +27,13 @@ jobs:
2727
- name: "Install Flutter"
2828
run: ./.github/workflows/scripts/install-flutter.sh stable
2929
- name: "Install Tools"
30-
run: ./.github/workflows/scripts/install-tools.sh
30+
run: |
31+
./.github/workflows/scripts/install-tools.sh
32+
sudo npm i -g firebase-tools
3133
- name: "Build Example"
3234
run: ./.github/workflows/scripts/build-example.sh android
35+
- name: Start Firebase Emulator
36+
run: cd ./.github/workflows/scripts && ./start-firebase-emulator.sh
3337
- name: "Drive Example"
3438
uses: reactivecircus/android-emulator-runner@v2
3539
with:
@@ -51,9 +55,12 @@ jobs:
5155
- name: "Install Tools"
5256
run: |
5357
./.github/workflows/scripts/install-tools.sh
58+
sudo npm i -g firebase-tools
5459
flutter config --enable-macos-desktop
5560
- name: "Build iOS Example"
5661
run: ./.github/workflows/scripts/build-example.sh ios
62+
- name: Start Firebase Emulator
63+
run: cd ./.github/workflows/scripts && ./start-firebase-emulator.sh
5764
- name: "Drive iOS Example"
5865
run: ./.github/workflows/scripts/drive-example.sh ios
5966
- name: "Build MacOS Example"
@@ -62,7 +69,9 @@ jobs:
6269
run: ./.github/workflows/scripts/drive-example.sh macos
6370

6471
web:
65-
runs-on: ubuntu-latest
72+
# Using macos instead of ubuntu as Web app can't connect to Firebase emulator
73+
# when running on ubuntu.
74+
runs-on: macos-latest
6675
timeout-minutes: 15
6776
steps:
6877
- uses: actions/checkout@v1
@@ -74,5 +83,8 @@ jobs:
7483
run: |
7584
./.github/workflows/scripts/install-tools.sh
7685
flutter config --enable-web
86+
sudo npm i -g firebase-tools
87+
- name: Start Firebase Emulator
88+
run: cd ./.github/workflows/scripts && ./start-firebase-emulator.sh
7789
- name: "Drive Example"
7890
run: ./.github/workflows/scripts/drive-example.sh web

.github/workflows/firebase_firestore.yaml

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ jobs:
3232
sudo npm i -g firebase-tools
3333
- name: "Build Example"
3434
run: ./.github/workflows/scripts/build-example.sh android
35-
- name: Start Firestore Emulator
36-
run: cd ./.github/workflows/scripts && ./start-firestore-emulator.sh
35+
- name: Start Firebase Emulator
36+
run: cd ./.github/workflows/scripts && ./start-firebase-emulator.sh
3737
- name: "Drive Example"
3838
uses: reactivecircus/android-emulator-runner@v2
3939
with:
@@ -61,8 +61,8 @@ jobs:
6161
flutter config --enable-macos-desktop
6262
- name: "Build iOS Example"
6363
run: ./.github/workflows/scripts/build-example.sh ios
64-
- name: Start Firestore Emulator
65-
run: cd ./.github/workflows/scripts && ./start-firestore-emulator.sh
64+
- name: Start Firebase Emulator
65+
run: cd ./.github/workflows/scripts && ./start-firebase-emulator.sh
6666
- name: "Drive iOS Example"
6767
run: ./.github/workflows/scripts/drive-example.sh ios
6868
- name: "Build MacOS Example"
@@ -73,7 +73,7 @@ jobs:
7373
run: ./.github/workflows/scripts/drive-example.sh macos
7474

7575
web:
76-
# Using macos instead of ubuntu as Web app can't connect to Firebase Firestore emulator
76+
# Using macos instead of ubuntu as Web app can't connect to Firebase emulator
7777
# when running on ubuntu.
7878
runs-on: macos-latest
7979
timeout-minutes: 15
@@ -88,7 +88,7 @@ jobs:
8888
./.github/workflows/scripts/install-tools.sh
8989
sudo npm i -g firebase-tools
9090
flutter config --enable-web
91-
- name: Start Firestore Emulator
92-
run: cd ./.github/workflows/scripts && ./start-firestore-emulator.sh
91+
- name: Start Firebase Emulator
92+
run: cd ./.github/workflows/scripts && ./start-firebase-emulator.sh
9393
- name: "Drive Example"
9494
run: ./.github/workflows/scripts/drive-example.sh web

.github/workflows/firebase_functions.yaml

+7-7
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ jobs:
3131
sudo npm i -g firebase-tools
3232
- name: "Build Example"
3333
run: ./.github/workflows/scripts/build-example.sh android
34-
- name: Start Functions Emulator
35-
run: cd ./.github/workflows/scripts && ./start-functions-emulator.sh
34+
- name: Start Firebase Emulator
35+
run: cd ./.github/workflows/scripts && ./start-firebase-emulator.sh
3636
- name: "Drive Example"
3737
uses: reactivecircus/android-emulator-runner@v2
3838
with:
@@ -60,8 +60,8 @@ jobs:
6060
sudo npm i -g firebase-tools
6161
- name: "Build iOS Example"
6262
run: ./.github/workflows/scripts/build-example.sh ios
63-
- name: Start Functions Emulator
64-
run: cd ./.github/workflows/scripts && ./start-functions-emulator.sh
63+
- name: Start Firebase Emulator
64+
run: cd ./.github/workflows/scripts && ./start-firebase-emulator.sh
6565
- name: "Drive iOS Example"
6666
run: ./.github/workflows/scripts/drive-example.sh ios
6767
- name: "Build MacOS Example"
@@ -70,7 +70,7 @@ jobs:
7070
run: ./.github/workflows/scripts/drive-example.sh macos
7171

7272
web:
73-
# Using macos instead of ubuntu as Web app can't connect to Firebase Functions emulator
73+
# Using macos instead of ubuntu as Web app can't connect to Firebase emulator
7474
# when running on ubuntu.
7575
runs-on: macos-latest
7676
timeout-minutes: 15
@@ -85,7 +85,7 @@ jobs:
8585
./.github/workflows/scripts/install-tools.sh
8686
flutter config --enable-web
8787
sudo npm i -g firebase-tools
88-
- name: Start Functions Emulator
89-
run: cd ./.github/workflows/scripts && ./start-functions-emulator.sh
88+
- name: Start Firebase Emulator
89+
run: cd ./.github/workflows/scripts && ./start-firebase-emulator.sh
9090
- name: "Drive Example"
9191
run: ./.github/workflows/scripts/drive-example.sh web

.github/workflows/scripts/firebase.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
"firestore": {
1010
"port": "8080"
1111
},
12+
"auth": {
13+
"port": "9099"
14+
},
1215
"ui": {
13-
"enabled": false
16+
"enabled": true,
17+
"port": 4000
1418
}
1519
}
1620
}

.github/workflows/scripts/start-functions-emulator.sh renamed to .github/workflows/scripts/start-firebase-emulator.sh

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ if [[ ! -d "functions/node_modules" ]]; then
1919
cd functions && npm i && cd ..
2020
fi
2121

22+
EMU_START_COMMAND="firebase emulators:start --only auth,firestore,functions --project react-native-firebase-testing"
23+
2224
IS_CI="${CI}${CONTINUOUS_INTEGRATION}${BUILD_NUMBER}${RUN_ID}"
2325
if [[ -n "${IS_CI}" ]]; then
24-
firebase emulators:start --only functions &
25-
until curl --output /dev/null --head --silent http://localhost:5001; do
26-
echo "Waiting for Functions emulator to come online..."
26+
$EMU_START_COMMAND &
27+
until curl --output /dev/null --silent --fail http://localhost:8080; do
28+
echo "Waiting for Firebase emulator to come online..."
2729
sleep 2
2830
done
29-
echo "Functions emulator is online!"
31+
echo "Firebase emulator is online!"
3032
else
31-
firebase emulators:start --only functions
33+
$EMU_START_COMMAND
3234
fi

.github/workflows/scripts/start-firestore-emulator.sh

-17
This file was deleted.

packages/firebase_auth/firebase_auth/android/src/main/java/io/flutter/plugins/firebase/auth/Constants.java

+2
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,6 @@ public class Constants {
7777
public static final String HANDLE_CODE_IN_APP = "handleCodeInApp";
7878
public static final String ACTION_CODE_SETTINGS = "actionCodeSettings";
7979
public static final String AUTO_RETRIEVED_SMS_CODE_FOR_TESTING = "autoRetrievedSmsCodeForTesting";
80+
public static final String HOST = "host";
81+
public static final String PORT = "port";
8082
}

packages/firebase_auth/firebase_auth/android/src/main/java/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.java

+15
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,18 @@ private Task<Void> signOut(Map<String, Object> arguments) {
764764
});
765765
}
766766

767+
private Task<Void> useEmulator(Map<String, Object> arguments) {
768+
return Tasks.call(
769+
cachedThreadPool,
770+
() -> {
771+
FirebaseAuth firebaseAuth = getAuth(arguments);
772+
String host = (String) arguments.get(Constants.HOST);
773+
int port = (int) arguments.get(Constants.PORT);
774+
firebaseAuth.useEmulator(host, port);
775+
return null;
776+
});
777+
}
778+
767779
private Task<Map<String, Object>> verifyPasswordResetCode(Map<String, Object> arguments) {
768780
return Tasks.call(
769781
cachedThreadPool,
@@ -1196,6 +1208,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
11961208
case "Auth#signOut":
11971209
methodCallTask = signOut(call.arguments());
11981210
break;
1211+
case "Auth#useEmulator":
1212+
methodCallTask = useEmulator(call.arguments());
1213+
break;
11991214
case "Auth#verifyPasswordResetCode":
12001215
methodCallTask = verifyPasswordResetCode(call.arguments());
12011216
break;

packages/firebase_auth/firebase_auth/example/android/app/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
additional functionality it is fine to subclass or reimplement
1010
FlutterApplication and put your custom class here. -->
1111
<application
12+
android:usesCleartextTraffic="true"
1213
android:name="io.flutter.app.FlutterApplication"
1314
android:label="firebaseauthexample"
1415
android:icon="@mipmap/ic_launcher">

packages/firebase_auth/firebase_auth/example/ios/Runner/Info.plist

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
<string>$(FLUTTER_BUILD_NUMBER)</string>
4242
<key>LSRequiresIPhoneOS</key>
4343
<true/>
44+
<key>NSAppTransportSecurity</key>
45+
<dict>
46+
<key>NSAllowsArbitraryLoads</key>
47+
<true/>
48+
</dict>
4449
<key>UILaunchStoryboardName</key>
4550
<string>LaunchScreen</string>
4651
<key>UIMainStoryboardFile</key>

packages/firebase_auth/firebase_auth/example/lib/main.dart

+13-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import './signin_page.dart';
1212
Future<void> main() async {
1313
WidgetsFlutterBinding.ensureInitialized();
1414
await Firebase.initializeApp();
15+
// Uncomment this to use the auth emulator for testing
16+
// await FirebaseAuth.instance.useEmulator('http://localhost:9099');
1517
runApp(AuthExampleApp());
1618
}
1719

@@ -22,11 +24,12 @@ class AuthExampleApp extends StatelessWidget {
2224
@override
2325
Widget build(BuildContext context) {
2426
return MaterialApp(
25-
title: 'Firebase Example App',
26-
theme: ThemeData.dark(),
27-
home: Scaffold(
28-
body: AuthTypeSelector(),
29-
));
27+
title: 'Firebase Example App',
28+
theme: ThemeData.dark(),
29+
home: Scaffold(
30+
body: AuthTypeSelector(),
31+
),
32+
);
3033
}
3134
}
3235

@@ -43,30 +46,30 @@ class AuthTypeSelector extends StatelessWidget {
4346
Widget build(BuildContext context) {
4447
return Scaffold(
4548
appBar: AppBar(
46-
title: Text("Firebase Example App"),
49+
title: const Text('Firebase Example App'),
4750
),
4851
body: Column(
4952
crossAxisAlignment: CrossAxisAlignment.start,
5053
children: <Widget>[
5154
Container(
55+
padding: const EdgeInsets.all(16),
56+
alignment: Alignment.center,
5257
child: SignInButtonBuilder(
5358
icon: Icons.person_add,
5459
backgroundColor: Colors.indigo,
5560
text: 'Registration',
5661
onPressed: () => _pushPage(context, RegisterPage()),
5762
),
58-
padding: const EdgeInsets.all(16),
59-
alignment: Alignment.center,
6063
),
6164
Container(
65+
padding: const EdgeInsets.all(16),
66+
alignment: Alignment.center,
6267
child: SignInButtonBuilder(
6368
icon: Icons.verified_user,
6469
backgroundColor: Colors.orange,
6570
text: 'Sign In',
6671
onPressed: () => _pushPage(context, SignInPage()),
6772
),
68-
padding: const EdgeInsets.all(16),
69-
alignment: Alignment.center,
7073
),
7174
],
7275
),

packages/firebase_auth/firebase_auth/example/test_driver/firebase_auth_e2e.dart

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// BSD-style license that can be found in the LICENSE file.
66

77
import 'package:drive/drive.dart' as drive;
8+
//import 'package:firebase_auth/firebase_auth.dart'; // only needed if you use the Auth Emulator
89
import 'package:firebase_core/firebase_core.dart';
910
import 'package:flutter_test/flutter_test.dart';
1011

@@ -17,6 +18,13 @@ bool USE_EMULATOR = false;
1718
void testsMain() {
1819
setUpAll(() async {
1920
await Firebase.initializeApp();
21+
22+
// Configure the Auth test suite to use the Auth Emulator
23+
// This may not be enabled until the test suite is ported to:
24+
// - have ability to create disabled users
25+
// - have ability to fetch OOB and SMS verification codes
26+
// JS implementation to port to dart here: https://github.com/invertase/react-native-firebase/pull/4552/commits/4c688413cb6516ecfdbd4ea325103d0d8d8d84a8#diff-44ccd5fb03b0d9e447820032866f2494c5a400a52873f0f65518d06aedafe302
27+
// await FirebaseAuth.instance.useEmulator('http://localhost:9099');
2028
});
2129

2230
runInstanceTests();

packages/firebase_auth/firebase_auth/ios/Classes/FLTFirebaseAuthPlugin.m

+8
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)flutter
212212
[self signInWithEmailLink:call.arguments withMethodCallResult:methodCallResult];
213213
} else if ([@"Auth#signOut" isEqualToString:call.method]) {
214214
[self signOut:call.arguments withMethodCallResult:methodCallResult];
215+
} else if ([@"Auth#useEmulator" isEqualToString:call.method]) {
216+
[self useEmulator:call.arguments withMethodCallResult:methodCallResult];
215217
} else if ([@"Auth#verifyPasswordResetCode" isEqualToString:call.method]) {
216218
[self verifyPasswordResetCode:call.arguments withMethodCallResult:methodCallResult];
217219
} else if ([@"Auth#verifyPhoneNumber" isEqualToString:call.method]) {
@@ -552,6 +554,12 @@ - (void)signOut:(id)arguments withMethodCallResult:(FLTFirebaseMethodCallResult
552554
}
553555
}
554556

557+
- (void)useEmulator:(id)arguments withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
558+
FIRAuth *auth = [self getFIRAuthFromArguments:arguments];
559+
[auth useEmulatorWithHost:arguments[@"host"] port:[arguments[@"port"] integerValue]];
560+
result.success(nil);
561+
}
562+
555563
- (void)verifyPasswordResetCode:(id)arguments
556564
withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
557565
FIRAuth *auth = [self getFIRAuthFromArguments:arguments];

packages/firebase_auth/firebase_auth/lib/firebase_auth.dart

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'dart:async';
99
import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart';
1010
import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
1111
import 'package:firebase_core/firebase_core.dart';
12+
import 'package:flutter/foundation.dart';
1213
import 'package:flutter/material.dart';
1314
import 'package:meta/meta.dart';
1415

0 commit comments

Comments
 (0)