Skip to content

Commit 10ef0bb

Browse files
authored
Add support of linkWithProvider and reauthenticateWithProvider (#3971)
1 parent 725fb86 commit 10ef0bb

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

Example/Auth/Sample/MainViewController+OAuth.m

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ - (StaticContentTableViewSection *)oAuthSection {
2929
return [StaticContentTableViewSection sectionWithTitle:@"OAuth" cells:@[
3030
[StaticContentTableViewCell cellWithTitle:@"Sign in with Google"
3131
action:^{ [weakSelf signInGoogleHeadfulLite]; }],
32+
[StaticContentTableViewCell cellWithTitle:@"Link with Google"
33+
action:^{ [weakSelf linkWithGoogleHeadfulLite]; }],
34+
[StaticContentTableViewCell cellWithTitle:@"Reauthenticate with Google"
35+
action:^{ [weakSelf reauthenticateWithGoogleHeadfulLite]; }],
3236
[StaticContentTableViewCell cellWithTitle:@"Sign in with Twitter"
3337
action:^{ [weakSelf signInTwitterHeadfulLite]; }],
3438
[StaticContentTableViewCell cellWithTitle:@"Sign in with GitHub"
@@ -75,6 +79,68 @@ - (void)signInGoogleHeadfulLite {
7579
}];
7680
}
7781

82+
- (void)linkWithGoogleHeadfulLite {
83+
FIROAuthProvider *provider = self.googleOAuthProvider;
84+
provider.customParameters = @{
85+
@"prompt" : @"consent",
86+
};
87+
provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
88+
[self showSpinner:^{
89+
[[AppManager auth].currentUser linkWithProvider:provider
90+
UIDelegate:nil
91+
completion:^(FIRAuthDataResult *_Nullable authResult,
92+
NSError *_Nullable error) {
93+
[self hideSpinner:^{
94+
if (error) {
95+
[self logFailure:@"Reauthenticate with provider (Google) failed" error:error];
96+
} else if (authResult.additionalUserInfo) {
97+
[self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
98+
if (self.isNewUserToggleOn) {
99+
NSString *newUserString = authResult.additionalUserInfo.newUser ?
100+
@"New user" : @"Existing user";
101+
[self showMessagePromptWithTitle:@"New or Existing"
102+
message:newUserString
103+
showCancelButton:NO
104+
completion:nil];
105+
}
106+
}
107+
[self showTypicalUIForUserUpdateResultsWithTitle:@"Link Error" error:error];
108+
}];
109+
}];
110+
}];
111+
}
112+
113+
- (void)reauthenticateWithGoogleHeadfulLite {
114+
FIROAuthProvider *provider = self.googleOAuthProvider;
115+
provider.customParameters = @{
116+
@"prompt" : @"consent",
117+
};
118+
provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
119+
[self showSpinner:^{
120+
[[AppManager auth].currentUser reauthenticateWithProvider:provider
121+
UIDelegate:nil
122+
completion:^(FIRAuthDataResult *_Nullable authResult,
123+
NSError *_Nullable error) {
124+
[self hideSpinner:^{
125+
if (error) {
126+
[self logFailure:@"Link with provider (Google) failed" error:error];
127+
} else if (authResult.additionalUserInfo) {
128+
[self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
129+
if (self.isNewUserToggleOn) {
130+
NSString *newUserString = authResult.additionalUserInfo.newUser ?
131+
@"New user" : @"Existing user";
132+
[self showMessagePromptWithTitle:@"New or Existing"
133+
message:newUserString
134+
showCancelButton:NO
135+
completion:nil];
136+
}
137+
}
138+
[self showTypicalUIForUserUpdateResultsWithTitle:@"Reauthenticate Error" error:error];
139+
}];
140+
}];
141+
}];
142+
}
143+
78144
- (void)signInTwitterHeadfulLite {
79145
FIROAuthProvider *provider = self.twitterOAuthProvider;
80146
[self showSpinner:^{

Firebase/Auth/Source/Public/FIRUser.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
@class FIRPhoneAuthCredential;
2525
@class FIRUserProfileChangeRequest;
2626
@class FIRUserMetadata;
27+
@protocol FIRAuthUIDelegate;
2728

2829
NS_ASSUME_NONNULL_BEGIN
2930

@@ -269,6 +270,22 @@ DEPRECATED_MSG_ATTRIBUTE( "Please use reauthenticateWithCredential:completion: f
269270
" Objective-C or reauthenticate(withCredential:completion:)"
270271
" for Swift instead.");
271272

273+
/** @fn reauthenticateWithProvider:UIDelegate:completion:
274+
@brief Renews the user's authentication using the provided auth provider instance.
275+
276+
@param provider An instance of an auth provider used to initiate the reauthenticate flow.
277+
@param UIDelegate Optionally an instance of a class conforming to the FIRAuthUIDelegate
278+
protocol, this is used for presenting the web context. If nil, a default FIRAuthUIDelegate
279+
will be used.
280+
@param completion Optionally; a block which is invoked when the reauthenticate flow finishes, or
281+
is canceled. Invoked asynchronously on the main thread in the future.
282+
*/
283+
- (void)reauthenticateWithProvider:(id<FIRFederatedAuthProvider>)provider
284+
UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
285+
completion:(nullable FIRAuthDataResultCallback)completion
286+
NS_SWIFT_NAME(reauthenticate(with:uiDelegate:completion:))
287+
API_AVAILABLE(ios(8.0));
288+
272289
/** @fn getIDTokenResultWithCompletion:
273290
@brief Retrieves the Firebase authentication token, possibly refreshing it if it has expired.
274291
@@ -359,6 +376,22 @@ DEPRECATED_MSG_ATTRIBUTE("Please use linkWithCredential:completion: for Objectiv
359376
- (void)linkWithCredential:(FIRAuthCredential *)credential
360377
completion:(nullable FIRAuthDataResultCallback)completion;
361378

379+
/** @fn linkWithProvider:UIDelegate:completion:
380+
@brief link the user with the provided auth provider instance.
381+
382+
@param provider An instance of an auth provider used to initiate the link flow.
383+
@param UIDelegate Optionally an instance of a class conforming to the FIRAuthUIDelegate
384+
protocol, this is used for presenting the web context. If nil, a default FIRAuthUIDelegate
385+
will be used.
386+
@param completion Optionally; a block which is invoked when the link flow finishes, or
387+
is canceled. Invoked asynchronously on the main thread in the future.
388+
*/
389+
- (void)linkWithProvider:(id<FIRFederatedAuthProvider>)provider
390+
UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
391+
completion:(nullable FIRAuthDataResultCallback)completion
392+
NS_SWIFT_NAME(link(with:uiDelegate:completion:))
393+
API_AVAILABLE(ios(8.0));
394+
362395
/** @fn unlinkFromProvider:completion:
363396
@brief Disassociates a user account from a third-party identity provider with this user.
364397

Firebase/Auth/Source/User/FIRUser.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#import "FIREmailAuthProvider.h"
3737
#import "FIREmailPasswordAuthCredential.h"
3838
#import "FIREmailLinkSignInRequest.h"
39+
#import "FIRFederatedAuthProvider.h"
3940
#import "FIRGameCenterAuthCredential.h"
4041
#import "FIRGetAccountInfoRequest.h"
4142
#import "FIRGetAccountInfoResponse.h"
@@ -791,6 +792,21 @@ - (void)reauthenticateAndRetrieveDataWithCredential:(FIRAuthCredential *) creden
791792
});
792793
}
793794

795+
- (void)reauthenticateWithProvider:(id<FIRFederatedAuthProvider>)provider
796+
UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
797+
completion:(nullable FIRAuthDataResultCallback)completion {
798+
#if TARGET_OS_IOS
799+
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
800+
[provider getCredentialWithUIDelegate:UIDelegate
801+
completion:^(FIRAuthCredential *_Nullable credential,
802+
NSError *_Nullable error) {
803+
[self reauthenticateWithCredential:credential
804+
completion:completion];
805+
}];
806+
});
807+
#endif // TARGET_OS_IOS
808+
}
809+
794810
- (nullable NSString *)refreshToken {
795811
__block NSString *result;
796812
dispatch_sync(FIRAuthGlobalWorkQueue(), ^{
@@ -1234,6 +1250,21 @@ - (void)linkAndRetrieveDataWithCredential:(FIRAuthCredential *)credential
12341250
});
12351251
}
12361252

1253+
- (void)linkWithProvider:(id<FIRFederatedAuthProvider>)provider
1254+
UIDelegate:(nullable id<FIRAuthUIDelegate>)UIDelegate
1255+
completion:(nullable FIRAuthDataResultCallback)completion {
1256+
#if TARGET_OS_IOS
1257+
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
1258+
[provider getCredentialWithUIDelegate:UIDelegate
1259+
completion:^(FIRAuthCredential *_Nullable credential,
1260+
NSError *_Nullable error) {
1261+
[self linkWithCredential:credential
1262+
completion:completion];
1263+
}];
1264+
});
1265+
#endif // TARGET_OS_IOS
1266+
}
1267+
12371268
- (void)unlinkFromProvider:(NSString *)provider
12381269
completion:(nullable FIRAuthResultCallback)completion {
12391270
[_taskQueue enqueueTask:^(FIRAuthSerialTaskCompletionBlock _Nonnull complete) {

0 commit comments

Comments
 (0)