diff --git a/packages/firebase_auth/firebase_auth/ios/firebase_auth/Sources/firebase_auth/FLTFirebaseAuthPlugin.m b/packages/firebase_auth/firebase_auth/ios/firebase_auth/Sources/firebase_auth/FLTFirebaseAuthPlugin.m index f42b49e8eb5d..7e7da1784579 100644 --- a/packages/firebase_auth/firebase_auth/ios/firebase_auth/Sources/firebase_auth/FLTFirebaseAuthPlugin.m +++ b/packages/firebase_auth/firebase_auth/ios/firebase_auth/Sources/firebase_auth/FLTFirebaseAuthPlugin.m @@ -602,51 +602,58 @@ static void handleAppleAuthResult(FLTFirebaseAuthPlugin *object, AuthPigeonFireb return; } if (credentials) { - [auth signInWithCredential:credentials - completion:^(FIRAuthDataResult *authResult, NSError *error) { - if (error != nil) { - NSDictionary *userInfo = [error userInfo]; - NSError *underlyingError = [userInfo objectForKey:NSUnderlyingErrorKey]; - - NSDictionary *firebaseDictionary = - underlyingError.userInfo[@"FIRAuthErrorUserInfoDes" - @"erializedResponseKey"]; - - if (firebaseDictionary == nil && - userInfo[@"FIRAuthErrorUserInfoNameKey"] != nil) { - // Removing since it's not parsed and causing issue when sending back the - // object to Flutter - NSMutableDictionary *mutableUserInfo = [userInfo mutableCopy]; - [mutableUserInfo - removeObjectForKey:@"FIRAuthErrorUserInfoUpdatedCredentialKey"]; - NSError *modifiedError = [NSError errorWithDomain:error.domain - code:error.code - userInfo:mutableUserInfo]; - - completion(nil, - [FlutterError errorWithCode:@"sign-in-failed" - message:userInfo[@"NSLocalizedDescription"] - details:modifiedError.userInfo]); - - } else if (firebaseDictionary != nil && - firebaseDictionary[@"message"] != nil) { - // error from firebase-ios-sdk is - // buried in underlying error. - completion(nil, - [FlutterError errorWithCode:@"sign-in-failed" - message:error.localizedDescription - details:firebaseDictionary[@"message"]]); - } else { - completion(nil, [FlutterError errorWithCode:@"sign-in-failed" - message:error.localizedDescription - details:error.userInfo]); + [auth + signInWithCredential:credentials + completion:^(FIRAuthDataResult *authResult, NSError *error) { + if (error != nil) { + NSDictionary *userInfo = [error userInfo]; + NSError *underlyingError = [userInfo objectForKey:NSUnderlyingErrorKey]; + + NSDictionary *firebaseDictionary = + underlyingError.userInfo[@"FIRAuthErrorUserInfoDes" + @"erializedResponseKey"]; + + NSString *errorCode = userInfo[@"FIRAuthErrorUserInfoNameKey"]; + + if (firebaseDictionary == nil && errorCode != nil) { + if ([errorCode isEqual:@"ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL"]) { + completion(nil, [FLTFirebaseAuthPlugin convertToFlutterError:error]); + return; } + + // Removing since it's not parsed and causing issue when sending back the + // object to Flutter + NSMutableDictionary *mutableUserInfo = [userInfo mutableCopy]; + [mutableUserInfo + removeObjectForKey:@"FIRAuthErrorUserInfoUpdatedCredentialKey"]; + NSError *modifiedError = [NSError errorWithDomain:error.domain + code:error.code + userInfo:mutableUserInfo]; + + completion(nil, + [FlutterError errorWithCode:@"sign-in-failed" + message:userInfo[@"NSLocalizedDescription"] + details:modifiedError.userInfo]); + + } else if (firebaseDictionary != nil && + firebaseDictionary[@"message"] != nil) { + // error from firebase-ios-sdk is + // buried in underlying error. + completion(nil, + [FlutterError errorWithCode:@"sign-in-failed" + message:error.localizedDescription + details:firebaseDictionary[@"message"]]); } else { - completion([PigeonParser getPigeonUserCredentialFromAuthResult:authResult - authorizationCode:nil], - nil); + completion(nil, [FlutterError errorWithCode:@"sign-in-failed" + message:error.localizedDescription + details:error.userInfo]); } - }]; + } else { + completion([PigeonParser getPigeonUserCredentialFromAuthResult:authResult + authorizationCode:nil], + nil); + } + }]; } }