Skip to content

Commit

Permalink
fix(auth): use correct app instance (vs always default) in multifacto…
Browse files Browse the repository at this point in the history
…r and phone auth (#7564)

* fix: fixed multifactor and phone auth always using default instance

---------

Co-authored-by: Mike Hardy <github@mikehardy.net>
  • Loading branch information
Somebodybro and mikehardy authored Jan 30, 2024
1 parent b5fd29c commit ff32fd3
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
113 changes: 69 additions & 44 deletions packages/auth/ios/RNFBAuth/RNFBAuthModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -426,7 +428,8 @@ - (void)invalidate {
FIRPhoneAuthCredential *credential =
(FIRPhoneAuthCredential *)[self getCredentialForProvider:provider
token:authToken
secret:authSecret];
secret:authSecret
firebaseApp:firebaseApp];

if (credential == nil) {
[RNFBSharedUtils
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 *)@{
Expand All @@ -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) {
Expand All @@ -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"]];
Expand All @@ -620,7 +623,7 @@ - (void)invalidate {
return;
}
if (credential) {
[[FIRAuth auth]
[[FIRAuth authWithApp:firebaseApp]
signInWithCredential:credential
completion:^(FIRAuthDataResult *_Nullable authResult,
NSError *_Nullable error) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -868,7 +872,8 @@ - (void)invalidate {
if (error) {
[self promiseRejectAuthException:reject error:error];
} else {
resolve(verificationID);
DLog(@"verificationID: %@", verificationID)
resolve(verificationID);
}
}];
}
Expand All @@ -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
Expand All @@ -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];
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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"]];
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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"]];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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];
Expand Down Expand Up @@ -1386,6 +1411,7 @@ - (NSDictionary *)multiFactorResolverToDict:(FIRMultiFactorResolver *)resolver {
return @{
@"hints" : [self convertMultiFactorData:resolver.hints],
@"session" : sessionHash,
@"auth" : resolver.auth
};
}

Expand All @@ -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
Expand Down Expand Up @@ -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])];
Expand Down Expand Up @@ -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,
}];
Expand Down Expand Up @@ -1720,4 +1745,4 @@ - (FIRActionCodeSettings *)buildActionCodeSettings:(NSDictionary *)actionCodeSet
return settings;
}

@end
@end
16 changes: 11 additions & 5 deletions packages/messaging/ios/RNFBMessaging/RNFBMessaging+AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,16 @@ - (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
#if __has_include(<FirebaseAuth/FirebaseAuth.h>)
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
Expand Down Expand Up @@ -237,4 +243,4 @@ - (void)application:(UIApplication *)application
}
}

@end
@end

0 comments on commit ff32fd3

Please sign in to comment.