3636 */
3737static 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 }
0 commit comments