MainViewController+Phone.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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+Phone.h"
  17. #import "AppManager.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. @implementation MainViewController (Phone)
  20. - (StaticContentTableViewSection *)phoneAuthSection {
  21. __weak typeof(self) weakSelf = self;
  22. return [StaticContentTableViewSection sectionWithTitle:@"Phone Auth" cells:@[
  23. [StaticContentTableViewCell cellWithTitle:@"Sign in With Phone Number"
  24. action:^{ [weakSelf signInWithPhoneNumberWithPrompt]; }],
  25. [StaticContentTableViewCell cellWithTitle:@"Link Phone Number"
  26. action:^{ [weakSelf linkPhoneNumberWithPrompt]; }],
  27. [StaticContentTableViewCell cellWithTitle:@"Unlink Phone Number"
  28. action:^{ [weakSelf unlinkFromProvider:FIRPhoneAuthProviderID completion:nil]; }],
  29. ]];
  30. }
  31. - (void)signInWithPhoneNumber:(NSString *_Nullable)phoneNumber
  32. completion:(nullable TestAutomationCallback)completion {
  33. [self showSpinner:^{
  34. [[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
  35. UIDelegate:nil
  36. completion:^(NSString *_Nullable verificationID,
  37. NSError *_Nullable error) {
  38. [self hideSpinner:^{
  39. if (error) {
  40. [self logFailure:@"failed to send verification code" error:error];
  41. [self showMessagePrompt:error.localizedDescription];
  42. if (completion) {
  43. completion(error);
  44. }
  45. return;
  46. }
  47. [self logSuccess:@"Code sent"];
  48. [self commonPhoneNumberInputWithTitle:@"Code"
  49. completion:^(NSString *_Nullable verificationCode) {
  50. [self commontPhoneVerificationWithVerificationID:verificationID
  51. verificationCode:verificationCode];
  52. if (completion) {
  53. completion(nil);
  54. }
  55. }];
  56. }];
  57. }];
  58. }];
  59. }
  60. - (void)signInWithPhoneNumberWithPrompt {
  61. [self commonPhoneNumberInputWithTitle:@"Phone #"
  62. completion:^(NSString *_Nullable phone) {
  63. [self signInWithPhoneNumber:phone completion:nil];
  64. }];
  65. }
  66. - (void)commonPhoneNumberInputWithTitle:(NSString *)title
  67. completion:(TextInputCompletionBlock)completion {
  68. [self showTextInputPromptWithMessage:title
  69. keyboardType:UIKeyboardTypePhonePad
  70. completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
  71. if (!userPressedOK) {
  72. return;
  73. }
  74. completion(phoneNumber);
  75. }];
  76. }
  77. - (void)commontPhoneVerificationWithVerificationID:(NSString *)verificationID
  78. verificationCode:(NSString *)verificationCode {
  79. [self showSpinner:^{
  80. FIRAuthCredential *credential =
  81. [[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
  82. verificationCode:verificationCode];
  83. [[AppManager auth] signInWithCredential:credential
  84. completion:^(FIRAuthDataResult *_Nullable result,
  85. NSError *_Nullable error) {
  86. [self hideSpinner:^{
  87. if (error) {
  88. [self logFailure:@"failed to verify phone number" error:error];
  89. [self showMessagePrompt:error.localizedDescription];
  90. return;
  91. }
  92. if (self.isNewUserToggleOn) {
  93. NSString *newUserString = result.additionalUserInfo.isNewUser ?
  94. @"New user" : @"Existing user";
  95. [self showMessagePromptWithTitle:@"New or Existing"
  96. message:newUserString
  97. showCancelButton:NO
  98. completion:nil];
  99. }
  100. }];
  101. }];
  102. }];
  103. }
  104. - (void)linkPhoneNumber:(NSString *_Nullable)phoneNumber
  105. completion:(nullable TestAutomationCallback)completion {
  106. [self showSpinner:^{
  107. [[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
  108. UIDelegate:nil
  109. completion:^(NSString *_Nullable verificationID,
  110. NSError *_Nullable error) {
  111. [self hideSpinner:^{
  112. if (error) {
  113. [self logFailure:@"failed to send verification code" error:error];
  114. [self showMessagePrompt:error.localizedDescription];
  115. if (completion) {
  116. completion(error);
  117. }
  118. return;
  119. }
  120. [self logSuccess:@"Code sent"];
  121. [self showTextInputPromptWithMessage:@"Verification code:"
  122. keyboardType:UIKeyboardTypeNumberPad
  123. completionBlock:^(BOOL userPressedOK,
  124. NSString *_Nullable verificationCode) {
  125. if (!userPressedOK || !verificationCode.length) {
  126. return;
  127. }
  128. [self showSpinner:^{
  129. FIRPhoneAuthCredential *credential =
  130. [[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
  131. verificationCode:verificationCode];
  132. [[self user] linkWithCredential:credential
  133. completion:^(FIRAuthDataResult *_Nullable result,
  134. NSError *_Nullable error) {
  135. [self hideSpinner:^{
  136. if (error) {
  137. if (error.code == FIRAuthErrorCodeCredentialAlreadyInUse) {
  138. [self showMessagePromptWithTitle:@"Phone number is already linked to "
  139. @"another user"
  140. message:@"Tap Ok to sign in with that user now."
  141. showCancelButton:YES
  142. completion:^(BOOL userPressedOK,
  143. NSString *_Nullable userInput) {
  144. if (userPressedOK) {
  145. // If FIRAuthErrorCodeCredentialAlreadyInUse error, sign in with the
  146. // provided credential.
  147. [self showSpinner:^{
  148. FIRPhoneAuthCredential *credential =
  149. error.userInfo[FIRAuthErrorUserInfoUpdatedCredentialKey];
  150. [[AppManager auth] signInWithCredential:credential
  151. completion:^(FIRAuthDataResult *_Nullable result,
  152. NSError *_Nullable error) {
  153. [self hideSpinner:^{
  154. if (error) {
  155. [self logFailure:@"failed to verify phone number" error:error];
  156. [self showMessagePrompt:error.localizedDescription];
  157. if (completion) {
  158. completion(error);
  159. }
  160. return;
  161. }
  162. }];
  163. }];
  164. }];
  165. }
  166. }];
  167. } else {
  168. [self logFailure:@"link phone number failed" error:error];
  169. [self showMessagePrompt:error.localizedDescription];
  170. }
  171. return;
  172. }
  173. [self logSuccess:@"link phone number succeeded."];
  174. if (completion) {
  175. completion(nil);
  176. }
  177. }];
  178. }];
  179. }];
  180. }];
  181. }];
  182. }];
  183. }];
  184. }
  185. - (void)linkPhoneNumberWithPrompt {
  186. [self showTextInputPromptWithMessage:@"Phone #:"
  187. keyboardType:UIKeyboardTypePhonePad
  188. completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
  189. if (!userPressedOK || !phoneNumber.length) {
  190. return;
  191. }
  192. [self linkPhoneNumber:phoneNumber completion:nil];
  193. }];
  194. }
  195. @end
  196. NS_ASSUME_NONNULL_END