GoogleAuthProvider.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "FIRApp.h"
  18. #import "FIROptions.h"
  19. #import "FIRGoogleAuthProvider.h"
  20. #import "ApplicationDelegate.h"
  21. @import GoogleSignIn;
  22. /** @typedef GoogleSignInCallback
  23. @brief The type of block invoked when a @c GIDGoogleUser object is ready or an error has
  24. occurred.
  25. @param user The Google user if any.
  26. @param error The error which occurred, if any.
  27. */
  28. typedef void (^GoogleSignInCallback)(GIDGoogleUser *user, NSError *error);
  29. /** @class GoogleAuthDelegate
  30. @brief The designated delegate class for Google Sign-In.
  31. @param callback A block which is invoked when the sign-in flow finishes. Invoked asynchronously
  32. on an unspecified thread in the future.
  33. */
  34. @interface GoogleAuthDelegate : NSObject <GIDSignInDelegate, GIDSignInUIDelegate, OpenURLDelegate>
  35. /** @fn initWithPresentingViewController:callback:
  36. @brief Initializes the new instance with the callback.
  37. @param presentingViewController The view controller to present the UI.
  38. @param callback A block which is invoked when the sign-in flow finishes. Invoked asynchronously
  39. on an unspecified thread in the future.
  40. */
  41. - (instancetype)initWithPresentingViewController:(UIViewController *)presentingViewController
  42. callback:(nullable GoogleSignInCallback)callback;
  43. @end
  44. @implementation GoogleAuthDelegate {
  45. UIViewController *_presentingViewController;
  46. GoogleSignInCallback _callback;
  47. }
  48. - (instancetype)initWithPresentingViewController:(UIViewController *)presentingViewController
  49. callback:(nullable GoogleSignInCallback)callback {
  50. self = [super init];
  51. if (self) {
  52. _presentingViewController = presentingViewController;
  53. _callback = callback;
  54. }
  55. return self;
  56. }
  57. - (void)signIn:(GIDSignIn *)signIn
  58. didSignInForUser:(GIDGoogleUser *)user
  59. withError:(NSError *)error {
  60. GoogleSignInCallback callback = _callback;
  61. _callback = nil;
  62. if (callback) {
  63. callback(user, error);
  64. }
  65. }
  66. - (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController {
  67. [_presentingViewController presentViewController:viewController animated:YES completion:nil];
  68. }
  69. - (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController {
  70. [_presentingViewController dismissViewControllerAnimated:YES completion:nil];
  71. }
  72. - (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
  73. return [[GIDSignIn sharedInstance] handleURL:url
  74. sourceApplication:sourceApplication
  75. annotation:nil];
  76. }
  77. @end
  78. @implementation GoogleAuthProvider
  79. - (void)getAuthCredentialWithPresentingViewController:(UIViewController *)viewController
  80. callback:(AuthCredentialCallback)callback {
  81. [self signOut];
  82. // The delegate needs to be retained.
  83. __block GoogleAuthDelegate *delegate = [[GoogleAuthDelegate alloc]
  84. initWithPresentingViewController:viewController
  85. callback:^(GIDGoogleUser *user, NSError *error) {
  86. [ApplicationDelegate setOpenURLDelegate:nil];
  87. delegate = nil;
  88. if (error) {
  89. callback(nil, error);
  90. return;
  91. }
  92. GIDAuthentication *auth = user.authentication;
  93. FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:auth.idToken
  94. accessToken:auth.accessToken];
  95. callback(credential, error);
  96. }];
  97. GIDSignIn *signIn = [GIDSignIn sharedInstance];
  98. signIn.clientID = [self googleClientID];
  99. signIn.shouldFetchBasicProfile = YES;
  100. signIn.delegate = delegate;
  101. signIn.uiDelegate = delegate;
  102. [ApplicationDelegate setOpenURLDelegate:delegate];
  103. [signIn signIn];
  104. }
  105. - (void)signOut {
  106. [[GIDSignIn sharedInstance] signOut];
  107. }
  108. - (NSString *)googleClientID {
  109. return [FIRApp defaultApp].options.clientID;
  110. }
  111. @end