From ff32fd37b39557e9a55fce016cbf986348436b92 Mon Sep 17 00:00:00 2001 From: Topias Nyquist <73340882+Somebodybro@users.noreply.github.com> Date: Tue, 30 Jan 2024 20:10:00 +0200 Subject: [PATCH] fix(auth): use correct app instance (vs always default) in multifactor and phone auth (#7564) * fix: fixed multifactor and phone auth always using default instance --------- Co-authored-by: Mike Hardy --- .../auth/ReactNativeFirebaseAuthModule.java | 10 +- packages/auth/ios/RNFBAuth/RNFBAuthModule.m | 113 +++++++++++------- .../RNFBMessaging/RNFBMessaging+AppDelegate.m | 16 ++- 3 files changed, 86 insertions(+), 53 deletions(-) diff --git a/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java b/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java index 4249379022..da134abad9 100644 --- a/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java +++ b/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java @@ -1140,10 +1140,11 @@ public void verifyPhoneNumberWithMultiFactorInfo( promise, "unknown", "Unsupported second factor. Only phone factors are supported."); return; } - + FirebaseApp firebaseApp = FirebaseApp.getInstance(appName); + FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp); final Activity activity = getCurrentActivity(); final PhoneAuthOptions phoneAuthOptions = - PhoneAuthOptions.newBuilder() + PhoneAuthOptions.newBuilder(firebaseAuth) .setActivity(activity) .setMultiFactorHint((PhoneMultiFactorInfo) selectedHint) .setTimeout(30L, TimeUnit.SECONDS) @@ -1184,9 +1185,10 @@ public void verifyPhoneNumberForMultiFactor( rejectPromiseWithCodeAndMessage(promise, "unknown", "can't find session for provided key"); return; } - + FirebaseApp firebaseApp = FirebaseApp.getInstance(appName); + FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp); final PhoneAuthOptions phoneAuthOptions = - PhoneAuthOptions.newBuilder() + PhoneAuthOptions.newBuilder(firebaseAuth) .setPhoneNumber(phoneNumber) .setActivity(getCurrentActivity()) .setTimeout(30L, TimeUnit.SECONDS) diff --git a/packages/auth/ios/RNFBAuth/RNFBAuthModule.m b/packages/auth/ios/RNFBAuth/RNFBAuthModule.m index 65e2f0b489..76862bc069 100644 --- a/packages/auth/ios/RNFBAuth/RNFBAuthModule.m +++ b/packages/auth/ios/RNFBAuth/RNFBAuthModule.m @@ -307,9 +307,8 @@ - (void)invalidate { : (RCTPromiseResolveBlock)resolve : (RCTPromiseRejectBlock)reject) { FIRUser *user = [FIRAuth authWithApp:firebaseApp].currentUser; - if (user) { - [self reloadAndReturnUser:user resolver:resolve rejecter:reject]; + [self reloadAndReturnUser:user resolver:resolve rejecter:reject firebaseApp:firebaseApp]; } else { [self promiseNoUser:resolve rejecter:reject isError:YES]; } @@ -383,7 +382,10 @@ - (void)invalidate { if (error) { [self promiseRejectAuthException:reject error:error]; } else { - [self reloadAndReturnUser:user resolver:resolve rejecter:reject]; + [self reloadAndReturnUser:user + resolver:resolve + rejecter:reject + firebaseApp:firebaseApp]; } }]; } else { @@ -426,7 +428,8 @@ - (void)invalidate { FIRPhoneAuthCredential *credential = (FIRPhoneAuthCredential *)[self getCredentialForProvider:provider token:authToken - secret:authSecret]; + secret:authSecret + firebaseApp:firebaseApp]; if (credential == nil) { [RNFBSharedUtils @@ -481,7 +484,7 @@ - (void)invalidate { if (error) { [self promiseRejectAuthException:reject error:error]; } else { - [self reloadAndReturnUser:user resolver:resolve rejecter:reject]; + [self reloadAndReturnUser:user resolver:resolve rejecter:reject firebaseApp:firebaseApp]; } }]; } else { @@ -565,8 +568,8 @@ - (void)invalidate { : (RCTPromiseRejectBlock)reject) { FIRAuthCredential *credential = [self getCredentialForProvider:provider token:authToken - secret:authSecret]; - + secret:authSecret + firebaseApp:firebaseApp]; if (credential == nil) { [RNFBSharedUtils rejectPromiseWithUserInfo:reject userInfo:(NSMutableDictionary *)@{ @@ -575,8 +578,7 @@ - (void)invalidate { @"has expired or is not currently supported.", }]; } - - [[FIRAuth authWithApp:firebaseApp] + DLog(@"using app SignInWithCredential: %@", firebaseApp.name)[[FIRAuth authWithApp:firebaseApp] signInWithCredential:credential completion:^(FIRAuthDataResult *authResult, NSError *error) { if (error) { @@ -602,7 +604,8 @@ - (void)invalidate { }]; } - __block FIROAuthProvider *builder = [FIROAuthProvider providerWithProviderID:providerId]; + __block FIROAuthProvider *builder = + [FIROAuthProvider providerWithProviderID:providerId auth:[FIRAuth authWithApp:firebaseApp]]; // Add scopes if present if (provider[@"scopes"]) { [builder setScopes:provider[@"scopes"]]; @@ -620,7 +623,7 @@ - (void)invalidate { return; } if (credential) { - [[FIRAuth auth] + [[FIRAuth authWithApp:firebaseApp] signInWithCredential:credential completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) { @@ -817,7 +820,8 @@ - (void)invalidate { : (NSString *)phoneNumber : (RCTPromiseResolveBlock)resolve : (RCTPromiseRejectBlock)reject) { - [[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]] + DLog(@"SignInWthPhoneNumber instance: %@", + firebaseApp.name)[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]] verifyPhoneNumber:phoneNumber UIDelegate:nil completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) { @@ -858,8 +862,8 @@ - (void)invalidate { }]; return; } - - [FIRPhoneAuthProvider.provider + DLog(@"using instance verifyPhoneNumberWithMultiFactorInfo: %@", + firebaseApp.name)[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]] verifyPhoneNumberWithMultiFactorInfo:hint UIDelegate:nil multiFactorSession:session @@ -868,7 +872,8 @@ - (void)invalidate { if (error) { [self promiseRejectAuthException:reject error:error]; } else { - resolve(verificationID); + DLog(@"verificationID: %@", verificationID) + resolve(verificationID); } }]; } @@ -880,7 +885,8 @@ - (void)invalidate { : (RCTPromiseResolveBlock)resolve : (RCTPromiseRejectBlock)reject) { FIRMultiFactorSession *session = cachedSessions[sessionId]; - [FIRPhoneAuthProvider.provider + DLog(@"using instance VerifyPhoneNumberForMultifactor: %@", + firebaseApp.name)[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]] verifyPhoneNumber:phoneNumber UIDelegate:nil multiFactorSession:session @@ -901,19 +907,22 @@ - (void)invalidate { : (NSString *)verificationCode : (RCTPromiseResolveBlock)resolve : (RCTPromiseRejectBlock)reject) { - FIRPhoneAuthCredential *credential = - [[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]] - credentialWithVerificationID:verificationId - verificationCode:verificationCode]; - FIRMultiFactorAssertion *assertion = + DLog(@"using instance resolve MultiFactorSignIn: %@", firebaseApp.name) + FIRPhoneAuthCredential *credential = + [[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]] + credentialWithVerificationID:verificationId + verificationCode:verificationCode]; + DLog(@"credential: %@", credential) FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential]; + [cachedResolver[sessionKey] resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) { - if (error) { + DLog(@"authError: %@", error) if (error) { [self promiseRejectAuthException:reject error:error]; - } else { + } + else { [self promiseWithAuthResult:resolve rejecter:reject authResult:authResult]; @@ -946,9 +955,11 @@ - (void)invalidate { : (NSString *_Nullable)displayName : (RCTPromiseResolveBlock)resolve : (RCTPromiseRejectBlock)reject) { - FIRPhoneAuthCredential *credential = - [FIRPhoneAuthProvider.provider credentialWithVerificationID:verificationId - verificationCode:verificationCode]; + DLog(@"using instance finalizeMultifactorEnrollment: %@", firebaseApp.name) + FIRPhoneAuthCredential *credential = + [[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]] + credentialWithVerificationID:verificationId + verificationCode:verificationCode]; FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential]; FIRUser *user = [FIRAuth authWithApp:firebaseApp].currentUser; @@ -969,12 +980,13 @@ - (void)invalidate { : (FIRApp *)firebaseApp : (NSString *)phoneNumber : (NSString *)requestKey) { - [FIRPhoneAuthProvider.provider + DLog(@"using instance verifyPhoneNumber: %@", + firebaseApp.name)[[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]] verifyPhoneNumber:phoneNumber UIDelegate:nil completion:^(NSString *_Nullable verificationID, NSError *_Nullable error) { if (error) { - NSDictionary *jsError = [self getJSError:(error)]; + NSDictionary *jsError = [self getJSError:error]; NSDictionary *body = @{ @"type" : @"onVerificationFailed", @"requestKey" : requestKey, @@ -1007,12 +1019,14 @@ - (void)invalidate { NSString *verificationId = [defaults stringForKey:@"authVerificationID"]; FIRAuthCredential *credential = - [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationId - verificationCode:verificationCode]; + [[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]] + credentialWithVerificationID:verificationId + verificationCode:verificationCode]; [[FIRAuth authWithApp:firebaseApp] signInWithCredential:credential completion:^(FIRAuthDataResult *authResult, NSError *error) { + DLog(@"auth error: %long", (long)error.code); if (error) { [self promiseRejectAuthException:reject error:error]; } else { @@ -1030,7 +1044,8 @@ - (void)invalidate { : (RCTPromiseRejectBlock)reject) { FIRAuthCredential *credential = [self getCredentialForProvider:provider token:authToken - secret:authSecret]; + secret:authSecret + firebaseApp:firebaseApp]; if (credential == nil) { [RNFBSharedUtils rejectPromiseWithUserInfo:reject @@ -1077,7 +1092,8 @@ - (void)invalidate { return; } - __block FIROAuthProvider *builder = [FIROAuthProvider providerWithProviderID:providerId]; + __block FIROAuthProvider *builder = + [FIROAuthProvider providerWithProviderID:providerId auth:[FIRAuth authWithApp:firebaseApp]]; // Add scopes if present if (provider[@"scopes"]) { [builder setScopes:provider[@"scopes"]]; @@ -1131,7 +1147,10 @@ - (void)invalidate { if (error) { [self promiseRejectAuthException:reject error:error]; } else { - [self reloadAndReturnUser:user resolver:resolve rejecter:reject]; + [self reloadAndReturnUser:user + resolver:resolve + rejecter:reject + firebaseApp:firebaseApp]; } }]; } else { @@ -1148,7 +1167,8 @@ - (void)invalidate { : (RCTPromiseRejectBlock)reject) { FIRAuthCredential *credential = [self getCredentialForProvider:provider token:authToken - secret:authSecret]; + secret:authSecret + firebaseApp:firebaseApp]; if (credential == nil) { [RNFBSharedUtils rejectPromiseWithUserInfo:reject @@ -1199,7 +1219,8 @@ - (void)invalidate { return; } - __block FIROAuthProvider *builder = [FIROAuthProvider providerWithProviderID:providerId]; + __block FIROAuthProvider *builder = + [FIROAuthProvider providerWithProviderID:providerId auth:[FIRAuth authWithApp:firebaseApp]]; // Add scopes if present if (provider[@"scopes"]) { [builder setScopes:provider[@"scopes"]]; @@ -1305,7 +1326,8 @@ - (void)invalidate { - (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider token:(NSString *)authToken - secret:(NSString *)authTokenSecret { + secret:(NSString *)authTokenSecret + firebaseApp:(FIRApp *)firebaseApp { FIRAuthCredential *credential; // First check if we cached an authToken @@ -1334,8 +1356,10 @@ - (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider } else if ([provider compare:@"github.com" options:NSCaseInsensitiveSearch] == NSOrderedSame) { credential = [FIRGitHubAuthProvider credentialWithToken:authToken]; } else if ([provider compare:@"phone" options:NSCaseInsensitiveSearch] == NSOrderedSame) { - credential = [[FIRPhoneAuthProvider provider] credentialWithVerificationID:authToken - verificationCode:authTokenSecret]; + DLog(@"using app credGen: %@", firebaseApp.name) credential = + [[FIRPhoneAuthProvider providerWithAuth:[FIRAuth authWithApp:firebaseApp]] + credentialWithVerificationID:authToken + verificationCode:authTokenSecret]; } else if ([provider compare:@"oauth" options:NSCaseInsensitiveSearch] == NSOrderedSame) { credential = [FIROAuthProvider credentialWithProviderID:@"oauth" IDToken:authToken @@ -1355,7 +1379,8 @@ - (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider // correctly refresh the user object when performing certain operations - (void)reloadAndReturnUser:(FIRUser *)user resolver:(RCTPromiseResolveBlock)resolve - rejecter:(RCTPromiseRejectBlock)reject { + rejecter:(RCTPromiseRejectBlock)reject + firebaseApp:(FIRApp *)firebaseApp { [user reloadWithCompletion:^(NSError *_Nullable error) { if (error) { [self promiseRejectAuthException:reject error:error]; @@ -1386,6 +1411,7 @@ - (NSDictionary *)multiFactorResolverToDict:(FIRMultiFactorResolver *)resolver { return @{ @"hints" : [self convertMultiFactorData:resolver.hints], @"session" : sessionHash, + @"auth" : resolver.auth }; } @@ -1399,7 +1425,7 @@ - (NSString *)getJSFactorId:(NSString *)factorId { } - (void)promiseRejectAuthException:(RCTPromiseRejectBlock)reject error:(NSError *)error { - NSDictionary *jsError = [self getJSError:(error)]; + NSDictionary *jsError = [self getJSError:error]; [RNFBSharedUtils rejectPromiseWithUserInfo:reject @@ -1499,8 +1525,7 @@ - (NSDictionary *)getJSError:(NSError *)error { NSDictionary *resolverDict = nil; if ([error userInfo][FIRAuthErrorUserInfoMultiFactorResolverKey] != nil) { - FIRMultiFactorResolver *resolver = - (FIRMultiFactorResolver *)error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey]; + FIRMultiFactorResolver *resolver = error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey]; resolverDict = [self multiFactorResolverToDict:resolver]; NSString *sessionKey = [NSString stringWithFormat:@"%@", @([resolver.session hash])]; @@ -1665,7 +1690,7 @@ - (NSDictionary *)firebaseUserToDict:(FIRUser *)user { [[[NSISO8601DateFormatter alloc] init] stringFromDate:hint.enrollmentDate]; [enrolledFactors addObject:@{ @"uid" : hint.UID, - @"factorId" : hint.factorID == nil ? [NSNull null] : [self getJSFactorId:(hint.factorID)], + @"factorId" : [self getJSFactorId:(hint.factorID)], @"displayName" : hint.displayName == nil ? [NSNull null] : hint.displayName, @"enrollmentDate" : enrollmentDate, }]; @@ -1720,4 +1745,4 @@ - (FIRActionCodeSettings *)buildActionCodeSettings:(NSDictionary *)actionCodeSet return settings; } -@end +@end \ No newline at end of file diff --git a/packages/messaging/ios/RNFBMessaging/RNFBMessaging+AppDelegate.m b/packages/messaging/ios/RNFBMessaging/RNFBMessaging+AppDelegate.m index 195579f30c..5c9c7d45f1 100644 --- a/packages/messaging/ios/RNFBMessaging/RNFBMessaging+AppDelegate.m +++ b/packages/messaging/ios/RNFBMessaging/RNFBMessaging+AppDelegate.m @@ -118,10 +118,16 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { #if __has_include() - if ([[FIRAuth auth] canHandleNotification:userInfo]) { - DLog(@"didReceiveRemoteNotification Firebase Auth handeled the notification"); - completionHandler(UIBackgroundFetchResultNoData); - return; + + for (id appId in [FIRApp allApps]) { + FIRApp *app = [[FIRApp allApps] objectForKey:appId]; + if ([[FIRAuth authWithApp:app] canHandleNotification:userInfo]) { + DLog( + @"didReceiveRemoteNotification Firebase Auth handeled the notification with instance: %@", + app.name); + completionHandler(UIBackgroundFetchResultNoData); + return; + } } // If the notification is a probe notification, always call the completion @@ -237,4 +243,4 @@ - (void)application:(UIApplication *)application } } -@end +@end \ No newline at end of file