MainViewController+Google.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "MainViewController+Google.h"
  17. #import "AuthProviders.h"
  18. #import "AppManager.h"
  19. #import "FIROAuthProvider.h"
  20. #import "MainViewController+Internal.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. @implementation MainViewController (Google)
  23. - (StaticContentTableViewSection *)googleAuthSection {
  24. __weak typeof(self) weakSelf = self;
  25. return [StaticContentTableViewSection sectionWithTitle:@"Google Auth" cells:@[
  26. [StaticContentTableViewCell cellWithTitle:@"Sign in with Google"
  27. action:^{ [weakSelf signInGoogle]; }],
  28. [StaticContentTableViewCell cellWithTitle:@"Link with Google"
  29. action:^{ [weakSelf linkWithGoogle]; }],
  30. [StaticContentTableViewCell cellWithTitle:@"Unlink from Google"
  31. action:^{ [weakSelf unlinkFromProvider:FIRGoogleAuthProviderID completion:nil]; }],
  32. [StaticContentTableViewCell cellWithTitle:@"Reauthenticate Google"
  33. action:^{ [weakSelf reauthenticateGoogle]; }],
  34. ]];
  35. }
  36. - (void)signInGoogle {
  37. FIRAuth *auth = [AppManager auth];
  38. if (!auth) {
  39. return;
  40. }
  41. [[AuthProviders google] getAuthCredentialWithPresentingViewController:self
  42. callback:^(FIRAuthCredential *credential,
  43. NSError *error) {
  44. if (credential) {
  45. FIRAuthDataResultCallback completion = ^(FIRAuthDataResult *_Nullable authResult,
  46. NSError *_Nullable error) {
  47. if (error) {
  48. [self logFailure:@"sign-in with provider failed" error:error];
  49. } else {
  50. [self logSuccess:@"sign-in with provider succeeded."];
  51. }
  52. if (authResult.additionalUserInfo) {
  53. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  54. if (self.isNewUserToggleOn) {
  55. NSString *newUserString = authResult.additionalUserInfo.isNewUser ?
  56. @"New user" : @"Existing user";
  57. [self showMessagePromptWithTitle:@"New or Existing"
  58. message:newUserString
  59. showCancelButton:NO
  60. completion:nil];
  61. }
  62. }
  63. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
  64. };
  65. [auth signInWithCredential:credential completion:completion];
  66. }
  67. }];
  68. }
  69. - (void)linkWithGoogle {
  70. [self linkWithAuthProvider:[AuthProviders google] retrieveData:NO];
  71. }
  72. - (void)reauthenticateGoogle {
  73. [self reauthenticate:[AuthProviders google] retrieveData:NO];
  74. }
  75. @end
  76. NS_ASSUME_NONNULL_END