Skip to content

Commit 8f64965

Browse files
committed
update FUIGoogleAuth for GoogleSignIn 6
1 parent 458dbb6 commit 8f64965

File tree

5 files changed

+84
-80
lines changed

5 files changed

+84
-80
lines changed

FirebaseGoogleAuthUI.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Pod::Spec.new do |s|
2121
s.dependency 'FirebaseAuth'
2222
s.dependency 'FirebaseCore'
2323
s.dependency 'FirebaseAuthUI'
24-
s.dependency 'GoogleSignIn', '~> 5.0'
24+
s.dependency 'GoogleSignIn', '~> 6.0'
2525
s.resource_bundles = {
2626
'FirebaseGoogleAuthUI' => ['FirebaseGoogleAuthUI/Sources/{Resources,Strings}/*.{png,lproj}']
2727
}

FirebaseGoogleAuthUI/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ platform :ios, '10.0'
66
target 'FirebaseGoogleAuthUI' do
77
use_frameworks!
88

9-
pod 'GoogleSignIn', '~> 5.0'
9+
pod 'GoogleSignIn', '~> 6.0'
1010
pod 'FirebaseAuthUI', :path => '../'
1111

1212
target 'FirebaseGoogleAuthUITests' do

FirebaseGoogleAuthUI/Podfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ PODS:
2525
- GoogleUtilities/Environment (~> 7.2)
2626
- nanopb (~> 2.30908.0)
2727
- PromisesObjC (< 3.0, >= 1.2)
28-
- GoogleSignIn (5.0.2):
29-
- AppAuth (~> 1.2)
28+
- GoogleSignIn (6.0.0):
29+
- AppAuth (~> 1.4)
3030
- GTMAppAuth (~> 1.0)
3131
- GTMSessionFetcher/Core (~> 1.1)
3232
- GoogleUtilities/AppDelegateSwizzler (7.5.0):
@@ -58,7 +58,7 @@ PODS:
5858

5959
DEPENDENCIES:
6060
- FirebaseAuthUI (from `../`)
61-
- GoogleSignIn (~> 5.0)
61+
- GoogleSignIn (~> 6.0)
6262
- OCMock
6363

6464
SPEC REPOS:
@@ -87,14 +87,14 @@ SPEC CHECKSUMS:
8787
FirebaseCore: a6dba751680d7033b9d3831e1cfc95ead0605118
8888
FirebaseCoreDiagnostics: 7e873baabcfaa9512f538554ae4fa0817aaafbdb
8989
GoogleDataTransport: 85fd18ff3019bb85d3f2c551d04c481dedf71fc9
90-
GoogleSignIn: 7137d297ddc022a7e0aa4619c86d72c909fa7213
90+
GoogleSignIn: b7779a809e1dfb32ebb43131b10d8bfcd53c1383
9191
GoogleUtilities: eea970f4a389963963bffe8d8fabe43540678b9c
9292
GTMAppAuth: ad5c2b70b9a8689e1a04033c9369c4915bfcbe89
9393
GTMSessionFetcher: 36689134877faeb055b27dfa4ccc9ceaa42e029e
9494
nanopb: a0ba3315591a9ae0a16a309ee504766e90db0c96
9595
OCMock: 29f6e52085b4e7d9b075cbf03ed7c3112f82f934
9696
PromisesObjC: 68159ce6952d93e17b2dfe273b8c40907db5ba58
9797

98-
PODFILE CHECKSUM: 5b245dea8385cbe6f02f214577a97083894700bf
98+
PODFILE CHECKSUM: 0ab8f1a707baefc68f0b4f5320060c1502798f29
9999

100100
COCOAPODS: 1.10.1

FirebaseGoogleAuthUI/Sources/FUIGoogleAuth.m

Lines changed: 65 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*/
3737
static NSString *const kSignInWithGoogle = @"SignInWithGoogle";
3838

39-
@interface FUIGoogleAuth () <GIDSignInDelegate>
39+
@interface FUIGoogleAuth ()
4040

4141
/** @property authUI
4242
@brief FUIAuth instance of the application.
@@ -50,16 +50,6 @@ @interface FUIGoogleAuth () <GIDSignInDelegate>
5050

5151
@end
5252
@implementation FUIGoogleAuth {
53-
/** @var _presentingViewController
54-
@brief The presenting view controller for interactive sign-in.
55-
*/
56-
UIViewController *_presentingViewController;
57-
58-
/** @var _pendingSignInCallback
59-
@brief The callback which should be invoked when the sign in flow completes (or is cancelled.)
60-
*/
61-
FUIAuthProviderSignInCompletionBlock _pendingSignInCallback;
62-
6353
/** @var _email
6454
@brief The email address associated with this account.
6555
*/
@@ -71,6 +61,10 @@ + (NSBundle *)bundle {
7161
inFrameworkBundle:[NSBundle bundleForClass:[self class]]];
7262
}
7363

64+
+ (NSArray<NSString *> *)defaultScopes {
65+
return @[kGoogleUserInfoEmailScope, kGoogleUserInfoProfileScope];
66+
}
67+
7468
- (instancetype)initWithAuthUI:(FUIAuth *)authUI {
7569
return [self initWithAuthUI:authUI scopes:@[kGoogleUserInfoEmailScope, kGoogleUserInfoProfileScope]];
7670
}
@@ -90,7 +84,7 @@ - (instancetype)initWithAuthUI:(FUIAuth *)authUI scopes:(NSArray<NSString *> *)s
9084
#pragma clang diagnostic push
9185
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
9286
- (instancetype)init {
93-
return [self initWithScopes:@[kGoogleUserInfoEmailScope, kGoogleUserInfoProfileScope]];
87+
return [self initWithScopes:[[self class] defaultScopes]];
9488
}
9589

9690
- (instancetype)initWithScopes:(NSArray *)scopes {
@@ -159,7 +153,6 @@ - (void)signInWithEmail:(nullable NSString *)email
159153
- (void)signInWithDefaultValue:(nullable NSString *)defaultValue
160154
presentingViewController:(nullable UIViewController *)presentingViewController
161155
completion:(nullable FUIAuthProviderSignInCompletionBlock)completion {
162-
_presentingViewController = presentingViewController;
163156

164157
if (self.authUI.isEmulatorEnabled) {
165158
[self signInWithOAuthProvider:self.providerForEmulator
@@ -168,26 +161,42 @@ - (void)signInWithDefaultValue:(nullable NSString *)defaultValue
168161
return;
169162
}
170163

171-
GIDSignIn *signIn = [self configuredGoogleSignIn];
172-
signIn.presentingViewController = presentingViewController;
173-
_pendingSignInCallback = ^(FIRAuthCredential *_Nullable credential,
164+
GIDSignIn *signIn = [GIDSignIn sharedInstance];
165+
NSString *clientID = self.authUI.auth.app.options.clientID;
166+
167+
if (!clientID) {
168+
[NSException raise:NSInternalInconsistencyException
169+
format:@"OAuth client ID not found. Please make sure Google Sign-In is enabled in "
170+
@"the Firebase console. You may have to download a new GoogleService-Info.plist file after "
171+
@"enabling Google Sign-In."];
172+
}
173+
174+
GIDConfiguration *config = [[GIDConfiguration alloc] initWithClientID:clientID];
175+
176+
FUIAuthProviderSignInCompletionBlock callback = ^(FIRAuthCredential *_Nullable credential,
174177
NSError *_Nullable error,
175178
_Nullable FIRAuthResultCallback result,
176179
NSDictionary *_Nullable userInfo) {
177-
signIn.loginHint = nil;
178180
if (completion) {
179181
completion(credential, error, result, nil);
180182
}
181183
};
182184

183-
signIn.loginHint = defaultValue;
184-
[signIn signIn];
185+
[signIn signInWithConfiguration:config
186+
presentingViewController:presentingViewController
187+
hint:defaultValue
188+
callback:^(GIDGoogleUser *user, NSError *error) {
189+
[self handleSignInWithUser:user
190+
error:error
191+
presentingViewController:presentingViewController
192+
callback:callback];
193+
}];
185194
}
186195

187196
- (void)signInWithOAuthProvider:(FIROAuthProvider *)oauthProvider
188197
presentingViewController:(nullable UIViewController *)presentingViewController
189198
completion:(nullable FUIAuthProviderSignInCompletionBlock)completion {
190-
oauthProvider.scopes = self.scopes;
199+
oauthProvider.scopes = [[self class] defaultScopes];
191200

192201
[oauthProvider getCredentialWithUIDelegate:nil
193202
completion:^(FIRAuthCredential *_Nullable credential,
@@ -214,92 +223,77 @@ - (void)signInWithOAuthProvider:(FIROAuthProvider *)oauthProvider
214223
}];
215224
}
216225

226+
- (void)requestScopesWithPresentingViewController:(UIViewController *)presentingViewController
227+
completion:(FUIAuthProviderSignInCompletionBlock)completion {
228+
GIDSignIn *signIn = [GIDSignIn sharedInstance];
229+
[signIn addScopes:self.scopes presentingViewController:presentingViewController
230+
callback:^(GIDGoogleUser *user, NSError *error) {
231+
[self handleSignInWithUser:user
232+
error:error
233+
presentingViewController:presentingViewController
234+
callback:^(FIRAuthCredential *credential,
235+
NSError *error,
236+
FIRAuthResultCallback result,
237+
NSDictionary<NSString *,id> *userInfo) {
238+
if (completion != nil) {
239+
completion(credential, error, result, nil);
240+
}
241+
}];
242+
}];
243+
}
244+
217245
- (void)signOut {
218246
if (self.authUI.isEmulatorEnabled) {
219247
return;
220248
}
221-
GIDSignIn *signIn = [self configuredGoogleSignIn];
249+
GIDSignIn *signIn = [GIDSignIn sharedInstance];
222250
[signIn signOut];
223251
}
224252

225253
- (BOOL)handleOpenURL:(NSURL *)URL sourceApplication:(NSString *)sourceApplication {
226254
if (self.authUI.isEmulatorEnabled) {
227255
return NO;
228256
}
229-
GIDSignIn *signIn = [self configuredGoogleSignIn];
257+
GIDSignIn *signIn = [GIDSignIn sharedInstance];
230258
return [signIn handleURL:URL];
231259
}
232260

233261
- (NSString *)email {
234262
return _email;
235263
}
236264

237-
#pragma mark - GIDSignInDelegate methods
238-
239-
- (void)signIn:(GIDSignIn *)signIn
240-
didSignInForUser:(GIDGoogleUser *)user
241-
withError:(NSError *)error {
265+
- (void)handleSignInWithUser:(GIDGoogleUser *)user
266+
error:(NSError *)error
267+
presentingViewController:(UIViewController *)presentingViewController
268+
callback:(FUIAuthProviderSignInCompletionBlock)callback {
242269
if (error) {
243270
if (error.code == kGIDSignInErrorCodeCanceled) {
244-
[self callbackWithCredential:nil
245-
error:[FUIAuthErrorUtils
246-
userCancelledSignInError] result:nil];
271+
NSError *newError = [FUIAuthErrorUtils userCancelledSignInError];
272+
if (callback) {
273+
callback(nil, newError, nil, nil);
274+
}
247275
} else {
248276
NSError *newError =
249277
[FUIAuthErrorUtils providerErrorWithUnderlyingError:error
250278
providerID:FIRGoogleAuthProviderID];
251-
[self callbackWithCredential:nil error:newError result:nil];
279+
if (callback) {
280+
callback(nil, newError, nil, nil);
281+
}
252282
}
253283
return;
254284
}
255285
_email = user.profile.email;
256286
UIActivityIndicatorView *activityView =
257-
[FUIAuthBaseViewController addActivityIndicator:_presentingViewController.view];
287+
[FUIAuthBaseViewController addActivityIndicator:presentingViewController.view];
258288
[activityView startAnimating];
259289
FIRAuthCredential *credential =
260290
[FIRGoogleAuthProvider credentialWithIDToken:user.authentication.idToken
261291
accessToken:user.authentication.accessToken];
262-
[self callbackWithCredential:credential error:nil result:^(FIRUser *_Nullable user,
263-
NSError *_Nullable error) {
292+
FIRAuthResultCallback result = ^(FIRUser *_Nullable user,
293+
NSError *_Nullable error) {
264294
[activityView stopAnimating];
265295
[activityView removeFromSuperview];
266-
}];
267-
}
268-
269-
#pragma mark - Helpers
270-
271-
/** @fn configuredGoogleSignIn
272-
@brief Returns an instance of @c GIDSignIn which is configured to match the configuration
273-
of this instance.
274-
*/
275-
- (GIDSignIn *)configuredGoogleSignIn {
276-
GIDSignIn *signIn = [GIDSignIn sharedInstance];
277-
signIn.delegate = self;
278-
signIn.shouldFetchBasicProfile = YES;
279-
signIn.clientID = [[FIRApp defaultApp] options].clientID;
280-
if (!signIn.clientID) {
281-
[NSException raise:NSInternalInconsistencyException
282-
format:@"OAuth client ID not found. Please make sure Google Sign-In is enabled in "
283-
@"the Firebase console. You may have to download a new GoogleService-Info.plist file after "
284-
@"enabling Google Sign-In."];
285-
}
286-
signIn.scopes = _scopes;
287-
return signIn;
288-
}
289-
290-
/** @fn callbackWithCredential:error:
291-
@brief Ends the sign-in flow by cleaning up and calling back with given credential or error.
292-
@param credential The credential to pass back, if any.
293-
@param error The error to pass back, if any.
294-
@param result The result of sign-in operation using provided @c FIRAuthCredential object.
295-
@see @c FIRAuth.signInWithCredential:completion:
296-
*/
297-
- (void)callbackWithCredential:(nullable FIRAuthCredential *)credential
298-
error:(nullable NSError *)error
299-
result:(nullable FIRAuthResultCallback)result {
300-
FUIAuthProviderSignInCompletionBlock callback = _pendingSignInCallback;
301-
_presentingViewController = nil;
302-
_pendingSignInCallback = nil;
296+
};
303297
if (callback) {
304298
callback(credential, error, result, nil);
305299
}

FirebaseGoogleAuthUI/Sources/Public/FirebaseGoogleAuthUI/FUIGoogleAuth.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ static NSString *const kGoogleUserInfoProfileScope = @"https://www.googleapis.co
4646
/** @property scopes
4747
@brief The scopes to use with Google Sign In.
4848
@remarks Defaults to using email and profile scopes. For a list of all scopes
49-
see https://developers.google.com/identity/protocols/googlescopes
49+
see https://developers.google.com/identity/protocols/googlescopes.
50+
Starting with GoogleSignIn 6.0, scopes are no longer granted upon first authentication and
51+
should be requested lazily.
5052
*/
5153
@property(nonatomic, copy, readonly) NSArray<NSString *> *scopes;
5254

@@ -66,7 +68,9 @@ static NSString *const kGoogleUserInfoProfileScope = @"https://www.googleapis.co
6668
@brief Designated initializer.
6769
@param authUI The @c FUIAuth instance that manages this provider.
6870
@param scopes The user account scopes required by the app. A list of possible scopes can be
69-
found at https://developers.google.com/identity/protocols/googlescopes
71+
found at https://developers.google.com/identity/protocols/googlescopes.
72+
Starting with GoogleSignIn 6.0, scopes are no longer granted upon first authentication and
73+
should be requested lazily.
7074
*/
7175
- (instancetype)initWithAuthUI:(FUIAuth *)authUI
7276
scopes:(NSArray <NSString *> *)scopes NS_DESIGNATED_INITIALIZER;
@@ -85,6 +89,12 @@ __attribute__((deprecated("Instead use initWithAuthUI:")));
8589
- (instancetype)initWithScopes:(NSArray <NSString *> *)scopes
8690
__attribute__((deprecated("Instead use initWithAuthUI:permissions:"))) NS_DESIGNATED_INITIALIZER;
8791

92+
/** @fn initWithAuthUI:scopes:
93+
@brief Requests the scopes in the `scopes` array.
94+
*/
95+
- (void)requestScopesWithPresentingViewController:(UIViewController *)presentingViewController
96+
completion:(FUIAuthProviderSignInCompletionBlock)completion;
97+
8898
@end
8999

90100
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)