|
18 | 18 |
|
19 | 19 | #import <FirebaseAuth/FirebaseAuth.h> |
20 | 20 | #import "FUIAuthBaseViewController_Internal.h" |
| 21 | +#import "FUIAuthErrorUtils.h" |
21 | 22 | #import "FUIAuthStrings.h" |
22 | 23 | #import "FUIAuthTableViewCell.h" |
23 | 24 | #import "FUIAuthUtils.h" |
24 | 25 | #import "FUIAuth_Internal.h" |
| 26 | +#import "FUIAuthErrors.h" |
25 | 27 | #import "FUIPasswordRecoveryViewController.h" |
26 | 28 |
|
27 | 29 | /** @var kCellReuseIdentifier |
@@ -108,35 +110,59 @@ - (void)signInWithDefaultValue:(NSString *)email andPassword:(NSString *)passwor |
108 | 110 | } |
109 | 111 |
|
110 | 112 | [self incrementActivity]; |
111 | | - |
112 | 113 | FIRAuthCredential *credential = |
113 | 114 | [FIREmailAuthProvider credentialWithEmail:email password:password]; |
114 | | - [self.auth signInAndRetrieveDataWithCredential:credential |
115 | | - completion:^(FIRAuthDataResult *_Nullable authResult, |
116 | | - NSError *_Nullable error) { |
117 | | - [self decrementActivity]; |
118 | | - |
119 | | - if (error) { |
120 | | - switch (error.code) { |
121 | | - case FIRAuthErrorCodeWrongPassword: |
122 | | - [self showAlertWithMessage:FUILocalizedString(kStr_WrongPasswordError)]; |
123 | | - return; |
124 | | - case FIRAuthErrorCodeUserNotFound: |
125 | | - [self showAlertWithMessage:FUILocalizedString(kStr_UserNotFoundError)]; |
126 | | - return; |
127 | | - case FIRAuthErrorCodeUserDisabled: |
128 | | - [self showAlertWithMessage:FUILocalizedString(kStr_AccountDisabledError)]; |
129 | | - return; |
130 | | - case FIRAuthErrorCodeTooManyRequests: |
131 | | - [self showAlertWithMessage:FUILocalizedString(kStr_SignInTooManyTimesError)]; |
| 115 | + |
| 116 | + void (^completeSignInBlock)(FIRAuthDataResult *, NSError *) = ^(FIRAuthDataResult *authResult, |
| 117 | + NSError *error) { |
| 118 | + [self decrementActivity]; |
| 119 | + |
| 120 | + if (error) { |
| 121 | + switch (error.code) { |
| 122 | + case FIRAuthErrorCodeWrongPassword: |
| 123 | + [self showAlertWithMessage:FUILocalizedString(kStr_WrongPasswordError)]; |
| 124 | + return; |
| 125 | + case FIRAuthErrorCodeUserNotFound: |
| 126 | + [self showAlertWithMessage:FUILocalizedString(kStr_UserNotFoundError)]; |
| 127 | + return; |
| 128 | + case FIRAuthErrorCodeUserDisabled: |
| 129 | + [self showAlertWithMessage:FUILocalizedString(kStr_AccountDisabledError)]; |
| 130 | + return; |
| 131 | + case FIRAuthErrorCodeTooManyRequests: |
| 132 | + [self showAlertWithMessage:FUILocalizedString(kStr_SignInTooManyTimesError)]; |
| 133 | + return; |
| 134 | + } |
| 135 | + } |
| 136 | + [self.navigationController dismissViewControllerAnimated:YES completion:^{ |
| 137 | + [self.authUI invokeResultCallbackWithAuthDataResult:authResult error:error]; |
| 138 | + }]; |
| 139 | + }; |
| 140 | + |
| 141 | + // Check for the presence of an anonymous user and whether automatic upgrade is enabled. |
| 142 | + if (self.auth.currentUser.isAnonymous && |
| 143 | + [FUIAuth defaultAuthUI].shouldAutoUpgradeAnonymousUsers) { |
| 144 | + |
| 145 | + [self.auth.currentUser |
| 146 | + linkAndRetrieveDataWithCredential:credential |
| 147 | + completion:^(FIRAuthDataResult *_Nullable authResult, |
| 148 | + NSError *_Nullable error) { |
| 149 | + if (error) { |
| 150 | + if (error.code == FIRAuthErrorCodeEmailAlreadyInUse) { |
| 151 | + NSDictionary *userInfo = @{ FUIAuthCredentialKey : credential }; |
| 152 | + NSError *mergeError = [FUIAuthErrorUtils mergeConflictErrorWithUserInfo:userInfo]; |
| 153 | + [self.navigationController dismissViewControllerAnimated:YES completion:^{ |
| 154 | + [self.authUI invokeResultCallbackWithAuthDataResult:authResult error:mergeError]; |
| 155 | + }]; |
132 | 156 | return; |
| 157 | + } |
| 158 | + completeSignInBlock(nil, error); |
| 159 | + return; |
133 | 160 | } |
134 | | - } |
135 | | - |
136 | | - [self.navigationController dismissViewControllerAnimated:YES completion:^{ |
137 | | - [self.authUI invokeResultCallbackWithAuthDataResult:authResult error:error]; |
| 161 | + completeSignInBlock(authResult, nil); |
138 | 162 | }]; |
139 | | - }]; |
| 163 | + } else { |
| 164 | + [self.auth signInAndRetrieveDataWithCredential:credential completion:completeSignInBlock]; |
| 165 | + } |
140 | 166 | } |
141 | 167 |
|
142 | 168 | - (void)signIn { |
|
0 commit comments