GoogleAuthProvider.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GoogleAuthProvider.h"
  17. #import <GoogleSignIn/GoogleSignIn.h>
  18. #import "AppManager.h"
  19. #import <FirebaseCore/FIRApp.h>
  20. #import <FirebaseCore/FIROptions.h>
  21. #import "FIRGoogleAuthProvider.h"
  22. #import "ApplicationDelegate.h"
  23. /** @typedef GoogleSignInCallback
  24. @brief The type of block invoked when a @c GIDGoogleUser object is ready or an error has
  25. occurred.
  26. @param user The Google user if any.
  27. @param error The error which occurred, if any.
  28. */
  29. typedef void (^GoogleSignInCallback)(GIDGoogleUser *user, NSError *error);
  30. /** @class GoogleAuthDelegate
  31. @brief The designated delegate class for Google Sign-In.
  32. */
  33. @interface GoogleAuthDelegate : NSObject <GIDSignInDelegate, OpenURLDelegate>
  34. /** @fn initWithPresentingViewController:callback:
  35. @brief Initializes the new instance with the callback.
  36. @param presentingViewController The view controller to present the UI.
  37. @param callback A block which is invoked when the sign-in flow finishes. Invoked asynchronously
  38. on an unspecified thread in the future.
  39. */
  40. - (instancetype)initWithPresentingViewController:(UIViewController *)presentingViewController
  41. callback:(nullable GoogleSignInCallback)callback;
  42. @end
  43. @implementation GoogleAuthDelegate {
  44. UIViewController *_presentingViewController;
  45. GoogleSignInCallback _callback;
  46. }
  47. - (instancetype)initWithPresentingViewController:(UIViewController *)presentingViewController
  48. callback:(nullable GoogleSignInCallback)callback {
  49. self = [super init];
  50. if (self) {
  51. _presentingViewController = presentingViewController;
  52. _callback = callback;
  53. }
  54. return self;
  55. }
  56. - (void)signIn:(GIDSignIn *)signIn
  57. didSignInForUser:(GIDGoogleUser *)user
  58. withError:(NSError *)error {
  59. GoogleSignInCallback callback = _callback;
  60. _callback = nil;
  61. if (callback) {
  62. callback(user, error);
  63. }
  64. }
  65. - (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
  66. return [[GIDSignIn sharedInstance] handleURL:url];
  67. }
  68. @end
  69. @implementation GoogleAuthProvider
  70. - (void)getAuthCredentialWithPresentingViewController:(UIViewController *)viewController
  71. callback:(AuthCredentialCallback)callback {
  72. [self signOut];
  73. // The delegate needs to be retained.
  74. __block GoogleAuthDelegate *delegate = [[GoogleAuthDelegate alloc]
  75. initWithPresentingViewController:viewController
  76. callback:^(GIDGoogleUser *user, NSError *error) {
  77. [ApplicationDelegate setOpenURLDelegate:nil];
  78. delegate = nil;
  79. if (error) {
  80. callback(nil, error);
  81. return;
  82. }
  83. GIDAuthentication *auth = user.authentication;
  84. FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:auth.idToken
  85. accessToken:auth.accessToken];
  86. callback(credential, error);
  87. }];
  88. GIDSignIn *signIn = [GIDSignIn sharedInstance];
  89. signIn.clientID = [self googleClientID];
  90. signIn.shouldFetchBasicProfile = YES;
  91. signIn.delegate = delegate;
  92. signIn.presentingViewController = viewController;
  93. [ApplicationDelegate setOpenURLDelegate:delegate];
  94. [signIn signIn];
  95. }
  96. - (void)signOut {
  97. [[GIDSignIn sharedInstance] signOut];
  98. }
  99. - (NSString *)googleClientID {
  100. return [AppManager app].options.clientID;
  101. }
  102. @end