Skip to content

Commit 13402d5

Browse files
committed
fix(auth, emulator): guard against double useEmulator calls
in case javascript hot reloads, track useEmulator calls natively and drop calls after the first one
1 parent 4e0d188 commit 13402d5

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class ReactNativeFirebaseAuthModule extends ReactNativeFirebaseModule {
7575
private static final String TAG = "Auth";
7676
private static HashMap<String, FirebaseAuth.AuthStateListener> mAuthListeners = new HashMap<>();
7777
private static HashMap<String, FirebaseAuth.IdTokenListener> mIdTokenListeners = new HashMap<>();
78+
private static HashMap<String, String> emulatorConfigs = new HashMap<>();
7879
private String mVerificationId;
7980
private String mLastPhoneNumber;
8081
private PhoneAuthProvider.ForceResendingToken mForceResendingToken;
@@ -1561,10 +1562,13 @@ public void verifyPasswordResetCode(String appName, String code, final Promise p
15611562

15621563
@ReactMethod
15631564
public void useEmulator(String appName, String host, int port) {
1564-
Log.d(TAG, "useEmulator");
1565-
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
1566-
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
1567-
firebaseAuth.useEmulator(host, port);
1565+
1566+
if (emulatorConfigs.get(appName) == null) {
1567+
emulatorConfigs.put(appName, "true");
1568+
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
1569+
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
1570+
firebaseAuth.useEmulator(host, port);
1571+
}
15681572
}
15691573

15701574
/* ------------------

packages/auth/ios/RNFBAuth/RNFBAuthModule.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
static __strong NSMutableDictionary *authStateHandlers;
5252
static __strong NSMutableDictionary *idTokenHandlers;
53+
static __strong NSMutableDictionary *emulatorConfigs;
5354
// Used for caching credentials between method calls.
5455
static __strong NSMutableDictionary<NSString *, FIRAuthCredential *> *credentials;
5556

@@ -69,6 +70,7 @@ - (id)init {
6970
dispatch_once(&onceToken, ^{
7071
authStateHandlers = [[NSMutableDictionary alloc] init];
7172
idTokenHandlers = [[NSMutableDictionary alloc] init];
73+
emulatorConfigs = [[NSMutableDictionary alloc] init];
7274
credentials = [[NSMutableDictionary alloc] init];
7375
});
7476
return self;
@@ -940,7 +942,10 @@ - (void)invalidate {
940942
: (FIRApp *)firebaseApp
941943
: (nonnull NSString *)host
942944
: (NSInteger)port) {
943-
[[FIRAuth authWithApp:firebaseApp] useEmulatorWithHost:host port:port];
945+
if (!emulatorConfigs[firebaseApp.name]) {
946+
[[FIRAuth authWithApp:firebaseApp] useEmulatorWithHost:host port:port];
947+
emulatorConfigs[firebaseApp.name] = @YES;
948+
}
944949
}
945950

946951
- (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider

0 commit comments

Comments
 (0)