GoogleAuthProvider.m 4.3 KB

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