Skip to content

Commit

Permalink
fix(auth, emulator): guard against double useEmulator calls
Browse files Browse the repository at this point in the history
in case javascript hot reloads, track useEmulator calls natively and
drop calls after the first one
  • Loading branch information
mikehardy committed Oct 19, 2022
1 parent 4e0d188 commit 13402d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class ReactNativeFirebaseAuthModule extends ReactNativeFirebaseModule {
private static final String TAG = "Auth";
private static HashMap<String, FirebaseAuth.AuthStateListener> mAuthListeners = new HashMap<>();
private static HashMap<String, FirebaseAuth.IdTokenListener> mIdTokenListeners = new HashMap<>();
private static HashMap<String, String> emulatorConfigs = new HashMap<>();
private String mVerificationId;
private String mLastPhoneNumber;
private PhoneAuthProvider.ForceResendingToken mForceResendingToken;
Expand Down Expand Up @@ -1561,10 +1562,13 @@ public void verifyPasswordResetCode(String appName, String code, final Promise p

@ReactMethod
public void useEmulator(String appName, String host, int port) {
Log.d(TAG, "useEmulator");
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
firebaseAuth.useEmulator(host, port);

if (emulatorConfigs.get(appName) == null) {
emulatorConfigs.put(appName, "true");
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
firebaseAuth.useEmulator(host, port);
}
}

/* ------------------
Expand Down
7 changes: 6 additions & 1 deletion packages/auth/ios/RNFBAuth/RNFBAuthModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

static __strong NSMutableDictionary *authStateHandlers;
static __strong NSMutableDictionary *idTokenHandlers;
static __strong NSMutableDictionary *emulatorConfigs;
// Used for caching credentials between method calls.
static __strong NSMutableDictionary<NSString *, FIRAuthCredential *> *credentials;

Expand All @@ -69,6 +70,7 @@ - (id)init {
dispatch_once(&onceToken, ^{
authStateHandlers = [[NSMutableDictionary alloc] init];
idTokenHandlers = [[NSMutableDictionary alloc] init];
emulatorConfigs = [[NSMutableDictionary alloc] init];
credentials = [[NSMutableDictionary alloc] init];
});
return self;
Expand Down Expand Up @@ -940,7 +942,10 @@ - (void)invalidate {
: (FIRApp *)firebaseApp
: (nonnull NSString *)host
: (NSInteger)port) {
[[FIRAuth authWithApp:firebaseApp] useEmulatorWithHost:host port:port];
if (!emulatorConfigs[firebaseApp.name]) {
[[FIRAuth authWithApp:firebaseApp] useEmulatorWithHost:host port:port];
emulatorConfigs[firebaseApp.name] = @YES;
}
}

- (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider
Expand Down

1 comment on commit 13402d5

@vercel
Copy link

@vercel vercel bot commented on 13402d5 Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.