Skip to content

Commit e2042d3

Browse files
committed
Implement additional FUIAuth sign-in call back which returns FIRAuthDataResult
1 parent 264202a commit e2042d3

File tree

14 files changed

+234
-195
lines changed

14 files changed

+234
-195
lines changed

FirebaseAuthUI/FUIAuth.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,36 @@ typedef void (^FUIAuthResultCallback)(FIRUser *_Nullable user, NSError *_Nullabl
4242
*/
4343
@protocol FUIAuthDelegate <NSObject>
4444

45-
/** @fn authUI:didSignInWithUser:error:
45+
@optional
46+
47+
/** @fn authUI:didSignInWithAuthDataResult:error:
4648
@brief Message sent after the sign in process has completed to report the signed in user or
4749
error encountered.
4850
@param authUI The @c FUIAuth instance sending the message.
51+
@param authDataResult The data result if the sign in attempt was successful.
52+
@param error The error that occurred during sign in, if any.
53+
*/
54+
- (void)authUI:(FUIAuth *)authUI
55+
didSignInWithAuthDataResult:(nullable FIRAuthDataResult *)authDataResult
56+
error:(nullable NSError *)error;
57+
58+
/** @fn authUI:didSignInWithUser:error:
59+
@brief This is deprecated API and will be removed in a future release.
60+
Use @c authUI:didSignInWithAuthDataResult:error:
61+
Both sign in call backs are called (@c authUI:didSignInWithAuthDataResult:error:
62+
and @c authUI:didSignInWithUser:error:).
63+
This message is sent after the sign in process has completed to report the signed in user or
64+
error encountered.
65+
@param authUI The @c FUIAuth instance sending the message.
4966
@param user The signed in user if the sign in attempt was successful.
5067
@param error The error that occurred during sign in, if any.
5168
*/
5269
- (void)authUI:(FUIAuth *)authUI
5370
didSignInWithUser:(nullable FIRUser *)user
54-
error:(nullable NSError *)error;
71+
error:(nullable NSError *)error
72+
__attribute__((deprecated("This is deprecated API and will be removed in a future release."
73+
"Use authUI:didSignInWithAuthDataResult:error:")));
5574

56-
@optional
5775

5876
/** @fn authUI:didFinishOperation:error:
5977
@brief Message sent after finishing Account Management operation.

FirebaseAuthUI/FUIAuth.m

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,17 @@ - (void)signInWithProviderUI:(id<FUIAuthProvider>)providerUI
166166
[presentingViewController isKindOfClass:[FUIAuthPickerViewController class]];
167167
if (error) {
168168
if (!isAuthPickerShown || error.code != FUIAuthErrorCodeUserCancelledSignIn) {
169-
[self invokeResultCallbackWithUser:nil error:error];
169+
[self invokeResultCallbackWithAuthDataResult:nil error:error];
170170
}
171171
if (result) {
172172
result(nil, error);
173173
}
174174
return;
175175
}
176176

177-
[self.auth signInWithCredential:credential
178-
completion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
177+
[self.auth signInAndRetrieveDataWithCredential:credential
178+
completion:^(FIRAuthDataResult *_Nullable authResult,
179+
NSError *_Nullable error) {
179180
if (error && error.code == FIRAuthErrorCodeAccountExistsWithDifferentCredential) {
180181
NSString *email = error.userInfo[kErrorUserInfoEmailKey];
181182
[self handleAccountLinkingForEmail:email
@@ -186,19 +187,19 @@ - (void)signInWithProviderUI:(id<FUIAuthProvider>)providerUI
186187
}
187188

188189
if (result) {
189-
result(user, error);
190+
result(authResult.user, error);
190191
}
191192

192193
if (error) {
193-
[self invokeResultCallbackWithUser:user error:error];
194+
[self invokeResultCallbackWithAuthDataResult:authResult error:error];
194195
} else {
195196
// Hide Auth Picker Controller which was presented modally.
196197
if (isAuthPickerShown && presentingViewController.presentingViewController) {
197198
[presentingViewController dismissViewControllerAnimated:YES completion:^{
198-
[self invokeResultCallbackWithUser:user error:error];
199+
[self invokeResultCallbackWithAuthDataResult:authResult error:error];
199200
}];
200201
} else {
201-
[self invokeResultCallbackWithUser:user error:error];
202+
[self invokeResultCallbackWithAuthDataResult:authResult error:error];
202203
}
203204
}
204205
}];
@@ -224,7 +225,7 @@ - (void)handleAccountLinkingForEmail:(NSString *)email
224225
presentingViewController:presentingViewController];
225226
} else {
226227
[presentingViewController dismissViewControllerAnimated:YES completion:^{
227-
[self invokeResultCallbackWithUser:nil error:error];
228+
[self invokeResultCallbackWithAuthDataResult:nil error:error];
228229
}];
229230
}
230231
return;
@@ -287,29 +288,30 @@ - (void)handleAccountLinkingForEmail:(NSString *)email
287288
}
288289
return;
289290
}
290-
[self invokeResultCallbackWithUser:nil error:error];
291+
[self invokeResultCallbackWithAuthDataResult:nil error:error];
291292
return;
292293
}
293294

294295
[self.auth signInWithCredential:credential completion:^(FIRUser *_Nullable user,
295296
NSError *_Nullable error) {
296297
if (error) {
297-
[self invokeResultCallbackWithUser:nil error:error];
298+
[self invokeResultCallbackWithAuthDataResult:nil error:error];
298299
if (result) {
299300
result(nil, error);
300301
}
301302
return;
302303
}
303304

304-
[user linkWithCredential:newCredential completion:^(FIRUser *_Nullable user,
305-
NSError *_Nullable error) {
305+
[user linkAndRetrieveDataWithCredential:newCredential
306+
completion:^(FIRAuthDataResult *_Nullable authResult,
307+
NSError *_Nullable error) {
306308
if (result) {
307-
result(user, error);
309+
result(authResult.user, error);
308310
}
309311
// Ignore any error (most likely caused by email mismatch) and treat the user as
310312
// successfully signed in.
311313
[presentingViewController dismissViewControllerAnimated:YES completion:^{
312-
[self invokeResultCallbackWithUser:user error:nil];
314+
[self invokeResultCallbackWithAuthDataResult:authResult error:nil];
313315
}];
314316
}];
315317
}];
@@ -322,9 +324,15 @@ - (void)handleAccountLinkingForEmail:(NSString *)email
322324

323325
#pragma mark - Internal Methods
324326

325-
- (void)invokeResultCallbackWithUser:(FIRUser *_Nullable)user error:(NSError *_Nullable)error {
327+
- (void)invokeResultCallbackWithAuthDataResult:(nullable FIRAuthDataResult *)authDataResult
328+
error:(nullable NSError *)error {
326329
dispatch_async(dispatch_get_main_queue(), ^{
327-
[self.delegate authUI:self didSignInWithUser:user error:error];
330+
if ([self.delegate respondsToSelector:@selector(authUI:didSignInWithAuthDataResult:error:)]) {
331+
[self.delegate authUI:self didSignInWithAuthDataResult:authDataResult error:error];
332+
}
333+
if ([self.delegate respondsToSelector:@selector(authUI:didSignInWithUser:error:)]) {
334+
[self.delegate authUI:self didSignInWithUser:authDataResult.user error:error];
335+
}
328336
});
329337
}
330338

FirebaseAuthUI/FUIAuthBaseViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ - (void)decrementActivity {
337337
- (void)cancelAuthorization {
338338
[self.navigationController dismissViewControllerAnimated:YES completion:^{
339339
NSError *error = [FUIAuthErrorUtils userCancelledSignInError];
340-
[self.authUI invokeResultCallbackWithUser:nil error:error];
340+
[self.authUI invokeResultCallbackWithAuthDataResult:nil error:error];
341341
}];
342342
}
343343

FirebaseAuthUI/FUIAuth_Internal.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ NS_ASSUME_NONNULL_BEGIN
2020

2121
@interface FUIAuth ()
2222

23-
/** @fn invokeResultCallbackWithUser:error:
23+
/** @fn invokeResultCallbackWithAuthDataResult:error:
2424
@brief Invokes the auth UI result callback.
25-
@param user The user signed in, if any.
25+
@param authDataResult The sign in data result, if any.
2626
@param error The error which occurred, if any.
2727
*/
28-
- (void)invokeResultCallbackWithUser:(FIRUser *_Nullable)user error:(NSError *_Nullable)error;
28+
- (void)invokeResultCallbackWithAuthDataResult:(nullable FIRAuthDataResult *)authDataResult
29+
error:(nullable NSError *)error;
2930

3031
/** @fn invokeOperationCallback:error:
3132
@brief Invokes the auth UI operation callback.
@@ -43,8 +44,8 @@ NS_ASSUME_NONNULL_BEGIN
4344
- (nullable id<FUIAuthProvider>)providerWithID:(NSString *)providerID;
4445

4546
/** @fn signInWithProviderUI:presentingViewController:defaultValue:
46-
@brief Signs in with specified provider. @see FUIAuthDelegate.authUI:didSignInWithUser:error:
47-
for method callback.
47+
@brief Signs in with specified provider.
48+
@see FUIAuthDelegate.authUI:didSignInWithAuthDataResult:error: for method callback.
4849
@param providerUI The authentication provider used for signing in.
4950
@param presentingViewController The view controller used to present the UI.
5051
@param defaultValue The provider default initialization value (e g email or phone number)

FirebaseAuthUI/FUIEmailEntryViewController.m

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ - (void)onNext:(NSString *)emailText {
139139
[self showAlertWithMessage:FUILocalizedString(kStr_InvalidEmailError)];
140140
} else {
141141
[self.navigationController dismissViewControllerAnimated:YES completion:^{
142-
[self.authUI invokeResultCallbackWithUser:nil error:error];
142+
[self.authUI invokeResultCallbackWithAuthDataResult:nil error:error];
143143
}];
144144
}
145145
return;
@@ -261,34 +261,35 @@ - (void)signInWithProvider:(id<FUIAuthProvider>)provider email:(NSString *)email
261261
// Sign out first to make sure sign in starts with a clean state.
262262
[provider signOut];
263263
[provider signInWithDefaultValue:email
264-
presentingViewController:self
265-
completion:^(FIRAuthCredential *_Nullable credential,
266-
NSError *_Nullable error,
267-
_Nullable FIRAuthResultCallback result) {
264+
presentingViewController:self
265+
completion:^(FIRAuthCredential *_Nullable credential,
266+
NSError *_Nullable error,
267+
_Nullable FIRAuthResultCallback result) {
268268
if (error) {
269269
[self decrementActivity];
270270
if (result) {
271271
result(nil, error);
272272
}
273273

274274
[self.navigationController dismissViewControllerAnimated:YES completion:^{
275-
[self.authUI invokeResultCallbackWithUser:nil error:error];
275+
[self.authUI invokeResultCallbackWithAuthDataResult:nil error:error];
276276
}];
277277
return;
278278
}
279279

280-
[self.auth signInWithCredential:credential
281-
completion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
280+
[self.auth signInAndRetrieveDataWithCredential:credential
281+
completion:^(FIRAuthDataResult *_Nullable authResult,
282+
NSError *_Nullable error) {
282283
[self decrementActivity];
283284
if (result) {
284-
result(user, error);
285+
result(authResult.user, error);
285286
}
286287

287288
if (error) {
288-
[self.authUI invokeResultCallbackWithUser:nil error:error];
289+
[self.authUI invokeResultCallbackWithAuthDataResult:nil error:error];
289290
} else {
290291
[self.navigationController dismissViewControllerAnimated:YES completion:^{
291-
[self.authUI invokeResultCallbackWithUser:user error:error];
292+
[self.authUI invokeResultCallbackWithAuthDataResult:authResult error:error];
292293
}];
293294
}
294295
}];

FirebaseAuthUI/FUIPasswordRecoveryViewController.m

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,28 @@ - (void)recoverEmail:(NSString *)email {
121121

122122
[self.auth sendPasswordResetWithEmail:email
123123
completion:^(NSError *_Nullable error) {
124-
// The dispatch is a workaround for a bug in FirebaseAuth 3.0.2, which doesn't call the
125-
// completion block on the main queue.
126-
dispatch_async(dispatch_get_main_queue(), ^{
127-
[self decrementActivity];
128-
129-
if (error) {
130-
if (error.code == FIRAuthErrorCodeUserNotFound) {
131-
[self showAlertWithMessage:FUILocalizedString(kStr_UserNotFoundError)];
132-
return;
133-
}
134-
135-
[self.navigationController dismissViewControllerAnimated:YES completion:^{
136-
[self.authUI invokeResultCallbackWithUser:nil error:error];
137-
}];
138-
return;
139-
}
140-
141-
NSString *message =
142-
[NSString stringWithFormat:FUILocalizedString(kStr_PasswordRecoveryEmailSentMessage), email];
143-
[self showAlertWithMessage:message];
144-
});
145-
}];
124+
// The dispatch is a workaround for a bug in FirebaseAuth 3.0.2, which doesn't call the
125+
// completion block on the main queue.
126+
dispatch_async(dispatch_get_main_queue(), ^{
127+
[self decrementActivity];
128+
129+
if (error) {
130+
if (error.code == FIRAuthErrorCodeUserNotFound) {
131+
[self showAlertWithMessage:FUILocalizedString(kStr_UserNotFoundError)];
132+
return;
133+
}
134+
135+
[self.navigationController dismissViewControllerAnimated:YES completion:^{
136+
[self.authUI invokeResultCallbackWithAuthDataResult:nil error:error];
137+
}];
138+
return;
139+
}
140+
141+
NSString *message =
142+
[NSString stringWithFormat:FUILocalizedString(kStr_PasswordRecoveryEmailSentMessage), email];
143+
[self showAlertWithMessage:message];
144+
});
145+
}];
146146
}
147147

148148
- (void)textFieldDidChange {

FirebaseAuthUI/FUIPasswordSignInViewController.m

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,34 @@ - (void)signInWithDefaultValue:(NSString *)email andPassword:(NSString *)passwor
109109

110110
[self incrementActivity];
111111

112-
[self.auth signInWithEmail:email
113-
password:password
114-
completion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
115-
[self decrementActivity];
116-
117-
if (error) {
118-
switch (error.code) {
119-
case FIRAuthErrorCodeWrongPassword:
120-
[self showAlertWithMessage:FUILocalizedString(kStr_WrongPasswordError)];
121-
return;
122-
case FIRAuthErrorCodeUserNotFound:
123-
[self showAlertWithMessage:FUILocalizedString(kStr_UserNotFoundError)];
124-
return;
125-
case FIRAuthErrorCodeUserDisabled:
126-
[self showAlertWithMessage:FUILocalizedString(kStr_AccountDisabledError)];
127-
return;
128-
case FIRAuthErrorCodeTooManyRequests:
129-
[self showAlertWithMessage:FUILocalizedString(kStr_SignInTooManyTimesError)];
130-
return;
131-
}
132-
}
133-
134-
[self.navigationController dismissViewControllerAnimated:YES completion:^{
135-
[self.authUI invokeResultCallbackWithUser:user error:error];
136-
}];
137-
}];
112+
FIRAuthCredential *credential =
113+
[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)];
132+
return;
133+
}
134+
}
135+
136+
[self.navigationController dismissViewControllerAnimated:YES completion:^{
137+
[self.authUI invokeResultCallbackWithAuthDataResult:authResult error:error];
138+
}];
139+
}];
138140
}
139141

140142
- (void)signIn {

0 commit comments

Comments
 (0)