MainViewController+Google.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "AppManager.h"
  18. #import "AuthProviders.h"
  19. #import "FIRMultiFactorResolver+Internal.h"
  20. #import "FIRMultiFactorSession+Internal.h"
  21. #import "FIROAuthProvider.h"
  22. #import "FIRPhoneMultiFactorInfo.h"
  23. #import "MainViewController+Internal.h"
  24. NS_ASSUME_NONNULL_BEGIN
  25. extern NSString *const FIRAuthErrorUserInfoMultiFactorResolverKey;
  26. @implementation MainViewController (Google)
  27. - (StaticContentTableViewSection *)googleAuthSection {
  28. __weak typeof(self) weakSelf = self;
  29. return [StaticContentTableViewSection sectionWithTitle:@"Google Auth" cells:@[
  30. [StaticContentTableViewCell cellWithTitle:@"Sign in with Google"
  31. action:^{ [weakSelf signInGoogle]; }],
  32. [StaticContentTableViewCell cellWithTitle:@"Link with Google"
  33. action:^{ [weakSelf linkWithGoogle]; }],
  34. [StaticContentTableViewCell cellWithTitle:@"Unlink from Google"
  35. action:^{ [weakSelf unlinkFromProvider:FIRGoogleAuthProviderID completion:nil]; }],
  36. [StaticContentTableViewCell cellWithTitle:@"Reauthenticate Google"
  37. action:^{ [weakSelf reauthenticateGoogle]; }],
  38. ]];
  39. }
  40. - (void)signInGoogle {
  41. FIRAuth *auth = [AppManager auth];
  42. if (!auth) {
  43. return;
  44. }
  45. [[AuthProviders google] getAuthCredentialWithPresentingViewController:self
  46. callback:^(FIRAuthCredential *credential,
  47. NSError *error) {
  48. if (credential) {
  49. FIRAuthDataResultCallback completion = ^(FIRAuthDataResult *_Nullable authResult,
  50. NSError *_Nullable error) {
  51. if (error) {
  52. if (error.code == FIRAuthErrorCodeSecondFactorRequired) {
  53. FIRMultiFactorResolver *resolver = error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey];
  54. NSMutableString *displayNameString = [NSMutableString string];
  55. for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) {
  56. [displayNameString appendString:tmpFactorInfo.displayName];
  57. [displayNameString appendString:@" "];
  58. }
  59. [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Select factor to sign in\n%@", displayNameString]
  60. completionBlock:^(BOOL userPressedOK, NSString *_Nullable displayName) {
  61. FIRPhoneMultiFactorInfo* selectedHint;
  62. for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) {
  63. if ([displayName isEqualToString:tmpFactorInfo.displayName]) {
  64. selectedHint = (FIRPhoneMultiFactorInfo *)tmpFactorInfo;
  65. }
  66. }
  67. [FIRPhoneAuthProvider.provider
  68. verifyPhoneNumberWithMultiFactorInfo:selectedHint
  69. UIDelegate:nil
  70. multiFactorSession:resolver.session
  71. completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
  72. if (error) {
  73. [self logFailure:@"Multi factor start sign in failed." error:error];
  74. } else {
  75. [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Verification code for %@", selectedHint.displayName]
  76. completionBlock:^(BOOL userPressedOK, NSString *_Nullable verificationCode) {
  77. FIRPhoneAuthCredential *credential =
  78. [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID
  79. verificationCode:verificationCode];
  80. FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
  81. [resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
  82. if (error) {
  83. [self logFailure:@"Multi factor finalize sign in failed." error:error];
  84. } else {
  85. [self logSuccess:@"Multi factor finalize sign in succeeded."];
  86. }
  87. }];
  88. }];
  89. }
  90. }];
  91. }];
  92. } else {
  93. [self logFailure:@"sign-in with provider failed" error:error];
  94. }
  95. } else {
  96. [self logSuccess:@"sign-in with provider succeeded."];
  97. }
  98. if (authResult.additionalUserInfo) {
  99. [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
  100. if (self.isNewUserToggleOn) {
  101. NSString *newUserString = authResult.additionalUserInfo.isNewUser ?
  102. @"New user" : @"Existing user";
  103. [self showMessagePromptWithTitle:@"New or Existing"
  104. message:newUserString
  105. showCancelButton:NO
  106. completion:nil];
  107. }
  108. }
  109. };
  110. [auth signInWithCredential:credential completion:completion];
  111. }
  112. }];
  113. }
  114. - (void)linkWithGoogle {
  115. [self linkWithAuthProvider:[AuthProviders google] retrieveData:NO];
  116. }
  117. - (void)reauthenticateGoogle {
  118. [self reauthenticate:[AuthProviders google] retrieveData:NO];
  119. }
  120. @end
  121. NS_ASSUME_NONNULL_END