Skip to content

Commit a69aa95

Browse files
authored
Merge pull request #375 from firebase/additionalData
Implement additional FUIAuth sign-in call back
2 parents 264202a + 32dc73b commit a69aa95

15 files changed

+252
-220
lines changed

FirebaseAuthUI/AccountManagement/FUIAccountSettingsOperationForgotPassword.m

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,26 @@ - (void)onPasswordRecovery:(NSString *)email {
6565

6666
[self.delegate.auth sendPasswordResetWithEmail:email
6767
completion:^(NSError *_Nullable error) {
68-
// The dispatch is a workaround for a bug in FirebaseAuth 3.0.2, which doesn't call the
69-
// completion block on the main queue.
70-
dispatch_async(dispatch_get_main_queue(), ^{
71-
[self.delegate decrementActivity];
68+
[self.delegate decrementActivity];
7269

73-
if (error) {
74-
[self finishOperationWithError:error];
75-
return;
76-
}
70+
if (error) {
71+
[self finishOperationWithError:error];
72+
return;
73+
}
7774

78-
NSString *message = [NSString stringWithFormat:
79-
FUILocalizedString(kStr_PasswordRecoveryEmailSentMessage), email];
80-
UIAlertController *alertController =
81-
[UIAlertController alertControllerWithTitle:nil
82-
message:message
83-
preferredStyle:UIAlertControllerStyleAlert];
84-
UIAlertAction *okAction = [UIAlertAction actionWithTitle:FUILocalizedString(kStr_OK)
85-
style:UIAlertActionStyleDefault
86-
handler:^(UIAlertAction *_Nonnull action) {
87-
[self finishOperationWithError:error];
88-
}];
89-
[alertController addAction:okAction];
90-
[self.delegate presentViewController:alertController];
91-
});
75+
NSString *message = [NSString stringWithFormat:
76+
FUILocalizedString(kStr_PasswordRecoveryEmailSentMessage), email];
77+
UIAlertController *alertController =
78+
[UIAlertController alertControllerWithTitle:nil
79+
message:message
80+
preferredStyle:UIAlertControllerStyleAlert];
81+
UIAlertAction *okAction = [UIAlertAction actionWithTitle:FUILocalizedString(kStr_OK)
82+
style:UIAlertActionStyleDefault
83+
handler:^(UIAlertAction *_Nonnull action) {
84+
[self finishOperationWithError:error];
85+
}];
86+
[alertController addAction:okAction];
87+
[self.delegate presentViewController:alertController];
9288
}];
9389
}
9490

FirebaseAuthUI/FUIAuth.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,35 @@ 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("Instead use authUI:didSignInWithAuthDataResult:error:")));
5573

56-
@optional
5774

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

FirebaseAuthUI/FUIAuth.m

Lines changed: 29 additions & 19 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
@@ -185,20 +186,22 @@ - (void)signInWithProviderUI:(id<FUIAuthProvider>)providerUI
185186
return;
186187
}
187188

188-
if (result) {
189-
result(user, error);
190-
}
191-
192189
if (error) {
193-
[self invokeResultCallbackWithUser:user error:error];
190+
if (result) {
191+
result(nil, error);
192+
}
193+
[self invokeResultCallbackWithAuthDataResult:nil error:error];
194194
} else {
195+
if (result) {
196+
result(authResult.user, nil);
197+
}
195198
// Hide Auth Picker Controller which was presented modally.
196199
if (isAuthPickerShown && presentingViewController.presentingViewController) {
197200
[presentingViewController dismissViewControllerAnimated:YES completion:^{
198-
[self invokeResultCallbackWithUser:user error:error];
201+
[self invokeResultCallbackWithAuthDataResult:authResult error:nil];
199202
}];
200203
} else {
201-
[self invokeResultCallbackWithUser:user error:error];
204+
[self invokeResultCallbackWithAuthDataResult:authResult error:nil];
202205
}
203206
}
204207
}];
@@ -224,7 +227,7 @@ - (void)handleAccountLinkingForEmail:(NSString *)email
224227
presentingViewController:presentingViewController];
225228
} else {
226229
[presentingViewController dismissViewControllerAnimated:YES completion:^{
227-
[self invokeResultCallbackWithUser:nil error:error];
230+
[self invokeResultCallbackWithAuthDataResult:nil error:error];
228231
}];
229232
}
230233
return;
@@ -287,29 +290,30 @@ - (void)handleAccountLinkingForEmail:(NSString *)email
287290
}
288291
return;
289292
}
290-
[self invokeResultCallbackWithUser:nil error:error];
293+
[self invokeResultCallbackWithAuthDataResult:nil error:error];
291294
return;
292295
}
293296

294297
[self.auth signInWithCredential:credential completion:^(FIRUser *_Nullable user,
295298
NSError *_Nullable error) {
296299
if (error) {
297-
[self invokeResultCallbackWithUser:nil error:error];
300+
[self invokeResultCallbackWithAuthDataResult:nil error:error];
298301
if (result) {
299302
result(nil, error);
300303
}
301304
return;
302305
}
303306

304-
[user linkWithCredential:newCredential completion:^(FIRUser *_Nullable user,
305-
NSError *_Nullable error) {
307+
[user linkAndRetrieveDataWithCredential:newCredential
308+
completion:^(FIRAuthDataResult *_Nullable authResult,
309+
NSError *_Nullable error) {
306310
if (result) {
307-
result(user, error);
311+
result(authResult.user, error);
308312
}
309313
// Ignore any error (most likely caused by email mismatch) and treat the user as
310314
// successfully signed in.
311315
[presentingViewController dismissViewControllerAnimated:YES completion:^{
312-
[self invokeResultCallbackWithUser:user error:nil];
316+
[self invokeResultCallbackWithAuthDataResult:authResult error:nil];
313317
}];
314318
}];
315319
}];
@@ -322,9 +326,15 @@ - (void)handleAccountLinkingForEmail:(NSString *)email
322326

323327
#pragma mark - Internal Methods
324328

325-
- (void)invokeResultCallbackWithUser:(FIRUser *_Nullable)user error:(NSError *_Nullable)error {
329+
- (void)invokeResultCallbackWithAuthDataResult:(nullable FIRAuthDataResult *)authDataResult
330+
error:(nullable NSError *)error {
326331
dispatch_async(dispatch_get_main_queue(), ^{
327-
[self.delegate authUI:self didSignInWithUser:user error:error];
332+
if ([self.delegate respondsToSelector:@selector(authUI:didSignInWithAuthDataResult:error:)]) {
333+
[self.delegate authUI:self didSignInWithAuthDataResult:authDataResult error:error];
334+
}
335+
if ([self.delegate respondsToSelector:@selector(authUI:didSignInWithUser:error:)]) {
336+
[self.delegate authUI:self didSignInWithUser:authDataResult.user error:error];
337+
}
328338
});
329339
}
330340

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: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,24 @@ - (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+
[self decrementActivity];
125+
126+
if (error) {
127+
if (error.code == FIRAuthErrorCodeUserNotFound) {
128+
[self showAlertWithMessage:FUILocalizedString(kStr_UserNotFoundError)];
129+
return;
130+
}
131+
132+
[self.navigationController dismissViewControllerAnimated:YES completion:^{
133+
[self.authUI invokeResultCallbackWithAuthDataResult:nil error:error];
134+
}];
135+
return;
136+
}
137+
138+
NSString *message = [NSString stringWithFormat:
139+
FUILocalizedString(kStr_PasswordRecoveryEmailSentMessage), email];
140+
[self showAlertWithMessage:message];
141+
}];
146142
}
147143

148144
- (void)textFieldDidChange {

0 commit comments

Comments
 (0)