|
14 | 14 | // limitations under the License. |
15 | 15 | // |
16 | 16 |
|
17 | | -#import "FUIPasswordSignInViewController.h" |
| 17 | +#import "FUIPasswordSignInViewController_Internal.h" |
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 |
@@ -78,6 +80,10 @@ - (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil |
78 | 80 | _email = [email copy]; |
79 | 81 |
|
80 | 82 | self.title = FUILocalizedString(kStr_SignInTitle); |
| 83 | + __weak FUIPasswordSignInViewController *weakself = self; |
| 84 | + _onDismissCallback = ^(FIRAuthDataResult *authResult, NSError *error){ |
| 85 | + [weakself.authUI invokeResultCallbackWithAuthDataResult:authResult error:error]; |
| 86 | + }; |
81 | 87 | } |
82 | 88 | return self; |
83 | 89 | } |
@@ -127,35 +133,61 @@ - (void)signInWithDefaultValue:(NSString *)email andPassword:(NSString *)passwor |
127 | 133 | } |
128 | 134 |
|
129 | 135 | [self incrementActivity]; |
130 | | - |
131 | 136 | FIRAuthCredential *credential = |
132 | 137 | [FIREmailAuthProvider credentialWithEmail:email password:password]; |
133 | | - [self.auth signInAndRetrieveDataWithCredential:credential |
134 | | - completion:^(FIRAuthDataResult *_Nullable authResult, |
135 | | - NSError *_Nullable error) { |
136 | | - [self decrementActivity]; |
137 | | - |
138 | | - if (error) { |
139 | | - switch (error.code) { |
140 | | - case FIRAuthErrorCodeWrongPassword: |
141 | | - [self showAlertWithMessage:FUILocalizedString(kStr_WrongPasswordError)]; |
142 | | - return; |
143 | | - case FIRAuthErrorCodeUserNotFound: |
144 | | - [self showAlertWithMessage:FUILocalizedString(kStr_UserNotFoundError)]; |
145 | | - return; |
146 | | - case FIRAuthErrorCodeUserDisabled: |
147 | | - [self showAlertWithMessage:FUILocalizedString(kStr_AccountDisabledError)]; |
148 | | - return; |
149 | | - case FIRAuthErrorCodeTooManyRequests: |
150 | | - [self showAlertWithMessage:FUILocalizedString(kStr_SignInTooManyTimesError)]; |
| 138 | + |
| 139 | + void (^completeSignInBlock)(FIRAuthDataResult *, NSError *) = ^(FIRAuthDataResult *authResult, |
| 140 | + NSError *error) { |
| 141 | + [self decrementActivity]; |
| 142 | + |
| 143 | + if (error) { |
| 144 | + switch (error.code) { |
| 145 | + case FIRAuthErrorCodeWrongPassword: |
| 146 | + [self showAlertWithMessage:FUILocalizedString(kStr_WrongPasswordError)]; |
| 147 | + return; |
| 148 | + case FIRAuthErrorCodeUserNotFound: |
| 149 | + [self showAlertWithMessage:FUILocalizedString(kStr_UserNotFoundError)]; |
| 150 | + return; |
| 151 | + case FIRAuthErrorCodeUserDisabled: |
| 152 | + [self showAlertWithMessage:FUILocalizedString(kStr_AccountDisabledError)]; |
| 153 | + return; |
| 154 | + case FIRAuthErrorCodeTooManyRequests: |
| 155 | + [self showAlertWithMessage:FUILocalizedString(kStr_SignInTooManyTimesError)]; |
| 156 | + return; |
| 157 | + } |
| 158 | + } |
| 159 | + [self.navigationController dismissViewControllerAnimated:YES completion:^{ |
| 160 | + if (self->_onDismissCallback) { |
| 161 | + self->_onDismissCallback(authResult, error); |
| 162 | + } |
| 163 | + }]; |
| 164 | + }; |
| 165 | + |
| 166 | + // Check for the presence of an anonymous user and whether automatic upgrade is enabled. |
| 167 | + if (self.auth.currentUser.isAnonymous && |
| 168 | + [FUIAuth defaultAuthUI].shouldAutoUpgradeAnonymousUsers) { |
| 169 | + |
| 170 | + [self.auth.currentUser |
| 171 | + linkAndRetrieveDataWithCredential:credential |
| 172 | + completion:^(FIRAuthDataResult *_Nullable authResult, |
| 173 | + NSError *_Nullable error) { |
| 174 | + if (error) { |
| 175 | + if (error.code == FIRAuthErrorCodeEmailAlreadyInUse) { |
| 176 | + NSDictionary *userInfo = @{ FUIAuthCredentialKey : credential }; |
| 177 | + NSError *mergeError = [FUIAuthErrorUtils mergeConflictErrorWithUserInfo:userInfo]; |
| 178 | + [self.navigationController dismissViewControllerAnimated:YES completion:^{ |
| 179 | + [self.authUI invokeResultCallbackWithAuthDataResult:authResult error:mergeError]; |
| 180 | + }]; |
151 | 181 | return; |
| 182 | + } |
| 183 | + completeSignInBlock(nil, error); |
| 184 | + return; |
152 | 185 | } |
153 | | - } |
154 | | - |
155 | | - [self dismissNavigationControllerAnimated:YES completion:^{ |
156 | | - [self.authUI invokeResultCallbackWithAuthDataResult:authResult error:error]; |
| 186 | + completeSignInBlock(authResult, nil); |
157 | 187 | }]; |
158 | | - }]; |
| 188 | + } else { |
| 189 | + [self.auth signInAndRetrieveDataWithCredential:credential completion:completeSignInBlock]; |
| 190 | + } |
159 | 191 | } |
160 | 192 |
|
161 | 193 | - (void)signIn { |
|
0 commit comments