Skip to content

Commit cb1fa84

Browse files
authored
Merge pull request #512 from morganchen12/anon
Improve error reporting in anonymous upgrade
2 parents 77e9ebc + b6aa989 commit cb1fa84

File tree

11 files changed

+133
-102
lines changed

11 files changed

+133
-102
lines changed

FirebaseAuthUI/FUIAuth.m

Lines changed: 121 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -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

FirebaseAuthUI/FUIAuthErrors.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ NS_ASSUME_NONNULL_BEGIN
2323
*/
2424
extern NSString *const FUIAuthErrorDomain;
2525

26-
/** @bar FUIAuthErrorUserInfoProviderIDKey
26+
/** @var FUIAuthErrorUserInfoProviderIDKey
2727
@brief The ID of the identity provider.
2828
*/
2929
extern NSString *const FUIAuthErrorUserInfoProviderIDKey;
3030

31-
/** @bar FUIAuthCredentialKey
31+
/** @var FUIAuthCredentialKey
3232
@brief The key used to obtain the credential stored within the userInfo dictionary of the
33-
error, if availalble.
33+
error, if available.
3434
*/
3535
extern NSString *const FUIAuthCredentialKey;
3636

FirebaseUI.xcodeproj/project.pbxproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
893D24852122014800CEF7D7 /* FUIAnonymousAuth.m in Sources */ = {isa = PBXBuildFile; fileRef = 893D247F2122014800CEF7D7 /* FUIAnonymousAuth.m */; };
1313
893D24862122014800CEF7D7 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 893D24802122014800CEF7D7 /* Info.plist */; };
1414
893D248F212202A400CEF7D7 /* FirebaseAnonymousAuthUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 893D248D212202A400CEF7D7 /* FirebaseAnonymousAuthUITests.m */; };
15-
893D2490212202A400CEF7D7 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 893D248E212202A400CEF7D7 /* Info.plist */; };
1615
894ED93E212211DE00CEF7D7 /* ic_anonymous.png in Resources */ = {isa = PBXBuildFile; fileRef = 894ED93B212211DE00CEF7D7 /* ic_anonymous.png */; };
1716
894ED93F212211DE00CEF7D7 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 894ED93C212211DE00CEF7D7 /* [email protected] */; };
1817
894ED940212211DE00CEF7D7 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 894ED93D212211DE00CEF7D7 /* [email protected] */; };
@@ -2502,7 +2501,7 @@
25022501
8D2A849D1D678B2B0058DF04 /* Project object */ = {
25032502
isa = PBXProject;
25042503
attributes = {
2505-
LastUpgradeCheck = 0930;
2504+
LastUpgradeCheck = 0940;
25062505
ORGANIZATIONNAME = FirebaseUI;
25072506
TargetAttributes = {
25082507
8D2A84A51D678B2B0058DF04 = {
@@ -2711,7 +2710,6 @@
27112710
isa = PBXResourcesBuildPhase;
27122711
buildActionMask = 2147483647;
27132712
files = (
2714-
893D2490212202A400CEF7D7 /* Info.plist in Resources */,
27152713
);
27162714
runOnlyForDeploymentPostprocessing = 0;
27172715
};

FirebaseUI.xcodeproj/xcshareddata/xcschemes/FirebaseAuthUI.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0930"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

FirebaseUI.xcodeproj/xcshareddata/xcschemes/FirebaseDatabaseUI.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0930"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

FirebaseUI.xcodeproj/xcshareddata/xcschemes/FirebaseFacebookAuthUI.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0930"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

FirebaseUI.xcodeproj/xcshareddata/xcschemes/FirebaseGoogleAuthUI.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0930"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

FirebaseUI.xcodeproj/xcshareddata/xcschemes/FirebaseStorageUI.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0930"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

FirebaseUI.xcodeproj/xcshareddata/xcschemes/FirebaseTwitterAuthUI.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0930"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

FirebaseUI.xcodeproj/xcshareddata/xcschemes/FirebaseUI.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0930"
3+
LastUpgradeVersion = "0940"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

0 commit comments

Comments
 (0)