@@ -199,21 +199,6 @@ - (void)signInWithProviderUI:(id<FUIAuthProvider>)providerUI
199199 }
200200 return ;
201201 }
202- // Block to complete sign-in
203- void (^completeSignInBlock)(FIRAuthDataResult *, NSError *) = ^(FIRAuthDataResult *authResult,
204- NSError *error) {
205- if (result) {
206- result (authResult.user , nil );
207- }
208- // Hide Auth Picker Controller which was presented modally.
209- if (isAuthPickerShown && presentingViewController.presentingViewController ) {
210- [presentingViewController dismissViewControllerAnimated: YES completion: ^{
211- [self invokeResultCallbackWithAuthDataResult: authResult error: error];
212- }];
213- } else {
214- [self invokeResultCallbackWithAuthDataResult: authResult error: error];
215- }
216- };
217202
218203 // Test if it's an anonymous login.
219204 if (self.auth .currentUser .isAnonymous && !credential) {
@@ -229,78 +214,10 @@ - (void)signInWithProviderUI:(id<FUIAuthProvider>)providerUI
229214
230215 // Check for the presence of an anonymous user and whether automatic upgrade is enabled.
231216 if (self.auth .currentUser .isAnonymous && self.shouldAutoUpgradeAnonymousUsers ) {
232- [self .auth.currentUser
233- linkAndRetrieveDataWithCredential: credential
234- completion: ^(FIRAuthDataResult *_Nullable authResult,
235- NSError * _Nullable error) {
236- if (error) {
237- // Check for "credential in use" conflict error and handle appropriately.
238- if (error.code == FIRAuthErrorCodeCredentialAlreadyInUse) {
239- FIRAuthCredential *newCredential = credential;
240- // Check for and handle special case for Phone Auth Provider.
241- if (providerUI.providerID == FIRPhoneAuthProviderID) {
242- // Obtain temporary Phone Auth credential.
243- newCredential = error.userInfo [FIRAuthUpdatedCredentialKey];
244- }
245- NSDictionary *userInfo = @{
246- FUIAuthCredentialKey : newCredential,
247- };
248- NSError *mergeError = [FUIAuthErrorUtils mergeConflictErrorWithUserInfo: userInfo
249- underlyingError: error];
250- completeSignInBlock (authResult, mergeError);
251- } else if (error.code == FIRAuthErrorCodeEmailAlreadyInUse) {
252- if ([providerUI respondsToSelector: @selector (email )]) {
253- // Link federated providers
254- [self signInWithEmailHint: [providerUI email ]
255- presentingViewController: presentingViewController
256- originalError: error
257- completion: ^(FIRAuthDataResult *_Nullable authResult,
258- NSError *_Nullable error,
259- FIRAuthCredential *_Nullable existingCredential) {
260- if (error) {
261- completeSignInBlock (nil , error);
262- return ;
263- }
264-
265- if (![authResult.user.email isEqualToString: [providerUI email ]]
266- && credential) {
267- NSDictionary *userInfo = @{
268- FUIAuthCredentialKey : credential,
269- };
270- NSError *mergeError = [FUIAuthErrorUtils mergeConflictErrorWithUserInfo: userInfo
271- underlyingError: nil ];
272- completeSignInBlock (nil , mergeError);
273- return ;
274- }
275-
276- [authResult.user linkAndRetrieveDataWithCredential: credential
277- completion: ^(FIRAuthDataResult
278- *_Nullable authResult,
279- NSError *_Nullable error) {
280- if (error) {
281- completeSignInBlock (nil , error);
282- return ;
283- }
284- FIRAuthCredential *newCredential = credential;
285- NSDictionary *userInfo = @{
286- FUIAuthCredentialKey : newCredential,
287- };
288- NSError *mergeError = [FUIAuthErrorUtils mergeConflictErrorWithUserInfo: userInfo
289- underlyingError: nil ];
290- completeSignInBlock (authResult, mergeError);
291- }];
292- }];
293- }
294- } else {
295- if (!isAuthPickerShown || error.code != FUIAuthErrorCodeUserCancelledSignIn) {
296- [self invokeResultCallbackWithAuthDataResult: nil error: error];
297- }
298- if (result) {
299- result (nil , error);
300- }
301- }
302- }
303- }];
217+ [self autoUpgradeAccountWithProviderUI: providerUI
218+ presentingViewController: presentingViewController
219+ credential: credential
220+ resultCallback: result];
304221 } else {
305222 [self .auth signInAndRetrieveDataWithCredential: credential
306223 completion: ^(FIRAuthDataResult *_Nullable authResult,
@@ -320,12 +237,128 @@ - (void)signInWithProviderUI:(id<FUIAuthProvider>)providerUI
320237 [self invokeResultCallbackWithAuthDataResult: nil error: error];
321238 return ;
322239 }
323- completeSignInBlock (authResult, nil );
240+ [self completeSignInWithResult: authResult
241+ error: nil
242+ presentingViewController: presentingViewController
243+ callback: result];
324244 }];
325245 }
326246 }];
327247}
328248
249+ - (void )autoUpgradeAccountWithProviderUI : (id <FUIAuthProvider>)providerUI
250+ presentingViewController : (FUIAuthBaseViewController *)presentingViewController
251+ credential : (nullable FIRAuthCredential *)credential
252+ resultCallback : (nullable FIRAuthResultCallback)callback {
253+ [self .auth.currentUser
254+ linkAndRetrieveDataWithCredential: credential
255+ completion: ^(FIRAuthDataResult *_Nullable authResult,
256+ NSError * _Nullable error) {
257+ if (error) {
258+ // Check for "credential in use" conflict error and handle appropriately.
259+ if (error.code == FIRAuthErrorCodeCredentialAlreadyInUse) {
260+ FIRAuthCredential *newCredential = credential;
261+ // Check for and handle special case for Phone Auth Provider.
262+ if (providerUI.providerID == FIRPhoneAuthProviderID) {
263+ // Obtain temporary Phone Auth credential.
264+ newCredential = error.userInfo [FIRAuthUpdatedCredentialKey];
265+ }
266+ NSDictionary *userInfo = @{
267+ FUIAuthCredentialKey : newCredential,
268+ };
269+ NSError *mergeError = [FUIAuthErrorUtils mergeConflictErrorWithUserInfo: userInfo
270+ underlyingError: error];
271+ [self completeSignInWithResult: authResult
272+ error: mergeError
273+ presentingViewController: presentingViewController
274+ callback: callback];
275+ } else if (error.code == FIRAuthErrorCodeEmailAlreadyInUse) {
276+ if ([providerUI respondsToSelector: @selector (email )]) {
277+ // Link federated providers
278+ [self signInWithEmailHint: [providerUI email ]
279+ presentingViewController: presentingViewController
280+ originalError: error
281+ completion: ^(FIRAuthDataResult *_Nullable authResult,
282+ NSError *_Nullable emailError,
283+ FIRAuthCredential *_Nullable existingCredential) {
284+ if (emailError) {
285+ [self completeSignInWithResult: nil
286+ error: emailError
287+ presentingViewController: presentingViewController
288+ callback: callback];
289+ return ;
290+ }
291+
292+ if (![authResult.user.email isEqualToString: [providerUI email ]]
293+ && credential) {
294+ NSDictionary *userInfo = @{
295+ FUIAuthCredentialKey : credential,
296+ };
297+ NSError *mergeError = [FUIAuthErrorUtils mergeConflictErrorWithUserInfo: userInfo
298+ underlyingError: error];
299+ [self completeSignInWithResult: nil
300+ error: mergeError
301+ presentingViewController: presentingViewController
302+ callback: callback];
303+ return ;
304+ }
305+
306+ [authResult.user linkAndRetrieveDataWithCredential: credential
307+ completion: ^(FIRAuthDataResult *authResult,
308+ NSError *linkError) {
309+ if (linkError) {
310+ [self completeSignInWithResult: nil
311+ error: linkError
312+ presentingViewController: presentingViewController
313+ callback: callback];
314+ return ;
315+ }
316+ FIRAuthCredential *newCredential = credential;
317+ NSDictionary *userInfo = @{
318+ FUIAuthCredentialKey : newCredential,
319+ };
320+ NSError *mergeError = [FUIAuthErrorUtils mergeConflictErrorWithUserInfo: userInfo
321+ underlyingError: error];
322+ [self completeSignInWithResult: authResult
323+ error: mergeError
324+ presentingViewController: presentingViewController
325+ callback: callback];
326+ }];
327+ }];
328+ }
329+ } else {
330+ BOOL isAuthPickerShown =
331+ [presentingViewController isKindOfClass: [FUIAuthPickerViewController class ]];
332+ if (!isAuthPickerShown || error.code != FUIAuthErrorCodeUserCancelledSignIn) {
333+ [self invokeResultCallbackWithAuthDataResult: nil error: error];
334+ }
335+ if (callback) {
336+ callback (nil , error);
337+ }
338+ }
339+ }
340+ }];
341+ }
342+
343+ - (void )completeSignInWithResult : (nullable FIRAuthDataResult *)authResult
344+ error : (nullable NSError *)error
345+ presentingViewController : (FUIAuthBaseViewController *)presentingViewController
346+ callback : (nullable FIRAuthResultCallback)callback {
347+ BOOL isAuthPickerShown =
348+ [presentingViewController isKindOfClass: [FUIAuthPickerViewController class ]];
349+ if (callback) {
350+ callback (authResult.user , nil );
351+ }
352+ // Hide Auth Picker Controller which was presented modally.
353+ if (isAuthPickerShown && presentingViewController.presentingViewController ) {
354+ [presentingViewController dismissViewControllerAnimated: YES completion: ^{
355+ [self invokeResultCallbackWithAuthDataResult: authResult error: error];
356+ }];
357+ } else {
358+ [self invokeResultCallbackWithAuthDataResult: authResult error: error];
359+ }
360+ }
361+
329362- (void )signInWithEmailHint : (NSString *)emailHint
330363 presentingViewController : (FUIAuthBaseViewController *)presentingViewController
331364 originalError : (NSError *)originalError
0 commit comments