GoogleAuthProvider.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2017 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, GIDSignInUIDelegate, 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. - (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController {
  66. [_presentingViewController presentViewController:viewController animated:YES completion:nil];
  67. }
  68. - (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController {
  69. [_presentingViewController dismissViewControllerAnimated:YES completion:nil];
  70. }
  71. - (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
  72. return [[GIDSignIn sharedInstance] handleURL:url
  73. sourceApplication:sourceApplication
  74. annotation:nil];
  75. }
  76. @end
  77. @implementation GoogleAuthProvider
  78. - (void)getAuthCredentialWithPresentingViewController:(UIViewController *)viewController
  79. callback:(AuthCredentialCallback)callback {
  80. [self signOut];
  81. // The delegate needs to be retained.
  82. __block GoogleAuthDelegate *delegate = [[GoogleAuthDelegate alloc]
  83. initWithPresentingViewController:viewController
  84. callback:^(GIDGoogleUser *user, NSError *error) {
  85. [ApplicationDelegate setOpenURLDelegate:nil];
  86. delegate = nil;
  87. if (error) {
  88. callback(nil, error);
  89. return;
  90. }
  91. GIDAuthentication *auth = user.authentication;
  92. FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:auth.idToken
  93. accessToken:auth.accessToken];
  94. callback(credential, error);
  95. }];
  96. GIDSignIn *signIn = [GIDSignIn sharedInstance];
  97. signIn.clientID = [self googleClientID];
  98. signIn.shouldFetchBasicProfile = YES;
  99. signIn.delegate = delegate;
  100. signIn.uiDelegate = delegate;
  101. [ApplicationDelegate setOpenURLDelegate:delegate];
  102. [signIn signIn];
  103. }
  104. - (void)signOut {
  105. [[GIDSignIn sharedInstance] signOut];
  106. }
  107. - (NSString *)googleClientID {
  108. return [AppManager app].options.clientID;
  109. }
  110. @end