MainViewController+User.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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+User.h"
  17. #import "AppManager.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. @implementation MainViewController (User)
  20. - (StaticContentTableViewSection *)userSection {
  21. __weak typeof(self) weakSelf = self;
  22. return [StaticContentTableViewSection sectionWithTitle:@"User" cells:@[
  23. [StaticContentTableViewCell cellWithTitle:@"Set Display Name"
  24. action:^{ [weakSelf setDisplayName]; }],
  25. [StaticContentTableViewCell cellWithTitle:@"Set Photo URL"
  26. action:^{ [weakSelf setPhotoURL]; }],
  27. [StaticContentTableViewCell cellWithTitle:@"Update Email"
  28. action:^{ [weakSelf updateEmail]; }],
  29. [StaticContentTableViewCell cellWithTitle:@"Update Password"
  30. action:^{ [weakSelf updatePassword]; }],
  31. [StaticContentTableViewCell cellWithTitle:@"Update Phone Number"
  32. action:^{ [weakSelf updatePhoneNumber]; }],
  33. [StaticContentTableViewCell cellWithTitle:@"Get Sign-in methods for Email"
  34. action:^{ [weakSelf getAllSignInMethodsForEmail]; }],
  35. [StaticContentTableViewCell cellWithTitle:@"Reload User"
  36. action:^{ [weakSelf reloadUser]; }],
  37. [StaticContentTableViewCell cellWithTitle:@"Delete User"
  38. action:^{ [weakSelf deleteAccount]; }],
  39. [StaticContentTableViewCell cellWithTitle:@"Verify before update email"
  40. action:^{ [weakSelf verifyBeforeUpdateEmail]; }],
  41. ]];
  42. }
  43. - (void)setDisplayName {
  44. [self showTextInputPromptWithMessage:@"Display Name:"
  45. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  46. if (!userPressedOK || !userInput.length) {
  47. return;
  48. }
  49. [self showSpinner:^{
  50. FIRUserProfileChangeRequest *changeRequest = [[self user] profileChangeRequest];
  51. changeRequest.displayName = userInput;
  52. [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
  53. [self hideSpinner:^{
  54. if (error) {
  55. [self logFailure:@"set display name failed" error:error];
  56. } else {
  57. [FIRAuth.auth.currentUser getIDTokenResultWithCompletion:^(FIRAuthTokenResult *_Nullable tokenResult,
  58. NSError *_Nullable error) {
  59. [self logSuccess:@"set display name succeeded."];
  60. }];
  61. }
  62. [self showTypicalUIForUserUpdateResultsWithTitle:@"Set Display Name" error:error];
  63. }];
  64. }];
  65. }];
  66. }];
  67. }
  68. - (void)setPhotoURL {
  69. [self showTextInputPromptWithMessage:@"Photo URL:"
  70. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  71. if (!userPressedOK || !userInput.length) {
  72. return;
  73. }
  74. [self showSpinner:^{
  75. FIRUserProfileChangeRequest *changeRequest = [[self user] profileChangeRequest];
  76. changeRequest.photoURL = [NSURL URLWithString:userInput];
  77. [changeRequest commitChangesWithCompletion:^(NSError *_Nullable error) {
  78. if (error) {
  79. [self logFailure:@"set photo URL failed" error:error];
  80. } else {
  81. [self logSuccess:@"set Photo URL succeeded."];
  82. }
  83. [self hideSpinner:^{
  84. [self showTypicalUIForUserUpdateResultsWithTitle:@"Set Photo URL" error:error];
  85. }];
  86. }];
  87. }];
  88. }];
  89. }
  90. - (void)reloadUser {
  91. [self showSpinner:^() {
  92. [[self user] reloadWithCompletion:^(NSError *_Nullable error) {
  93. if (error) {
  94. [self logFailure:@"reload user failed" error:error];
  95. } else {
  96. [self logSuccess:@"reload user succeeded."];
  97. }
  98. [self hideSpinner:^() {
  99. [self showTypicalUIForUserUpdateResultsWithTitle:@"Reload User" error:error];
  100. }];
  101. }];
  102. }];
  103. }
  104. - (void)getAllSignInMethodsForEmail {
  105. [self showTextInputPromptWithMessage:@"Email:"
  106. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  107. if (!userPressedOK || !userInput.length) {
  108. return;
  109. }
  110. [self showSpinner:^{
  111. [[AppManager auth] fetchSignInMethodsForEmail:userInput
  112. completion:^(NSArray<NSString *> *_Nullable signInMethods,
  113. NSError *_Nullable error) {
  114. if (error) {
  115. [self logFailure:@"get sign-in methods for email failed" error:error];
  116. } else {
  117. [self logSuccess:@"get sign-in methods for email succeeded."];
  118. }
  119. [self hideSpinner:^{
  120. if (error) {
  121. [self showMessagePrompt:error.localizedDescription];
  122. return;
  123. }
  124. [self showMessagePrompt:[signInMethods componentsJoinedByString:@", "]];
  125. }];
  126. }];
  127. }];
  128. }];
  129. }
  130. - (void)updateEmail {
  131. [self showTextInputPromptWithMessage:@"Email Address:"
  132. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  133. if (!userPressedOK || !userInput.length) {
  134. return;
  135. }
  136. [self showSpinner:^{
  137. [[self user] updateEmail:userInput completion:^(NSError *_Nullable error) {
  138. if (error) {
  139. [self logFailure:@"update email failed" error:error];
  140. } else {
  141. [self logSuccess:@"update email succeeded."];
  142. }
  143. [self hideSpinner:^{
  144. [self showTypicalUIForUserUpdateResultsWithTitle:@"Update Email" error:error];
  145. }];
  146. }];
  147. }];
  148. }];
  149. }
  150. - (void)verifyBeforeUpdateEmail {
  151. [self showTextInputPromptWithMessage:@"Email Address:"
  152. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  153. if (!userPressedOK || !userInput.length) {
  154. return;
  155. }
  156. [self showSpinner:^{
  157. [[self user] sendEmailVerificationBeforeUpdatingEmail:userInput
  158. actionCodeSettings:[self actionCodeSettings]
  159. completion:^(NSError *_Nullable error) {
  160. if (error) {
  161. [self logFailure:@"verify before update email failed." error:error];
  162. } else {
  163. [self logSuccess:@"verify before update email succeeded."];
  164. }
  165. [self hideSpinner:^{
  166. [self showTypicalUIForUserUpdateResultsWithTitle:@"Update Email" error:error];
  167. }];
  168. }];
  169. }];
  170. }];
  171. }
  172. - (void)updatePassword {
  173. [self showTextInputPromptWithMessage:@"New Password:"
  174. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  175. if (!userPressedOK) {
  176. return;
  177. }
  178. [self showSpinner:^{
  179. [[self user] updatePassword:userInput completion:^(NSError *_Nullable error) {
  180. if (error) {
  181. [self logFailure:@"update password failed" error:error];
  182. } else {
  183. [self logSuccess:@"update password succeeded."];
  184. }
  185. [self hideSpinner:^{
  186. [self showTypicalUIForUserUpdateResultsWithTitle:@"Update Password" error:error];
  187. }];
  188. }];
  189. }];
  190. }];
  191. }
  192. - (void)deleteAccount {
  193. FIRUser *user = [self user];
  194. [user deleteWithCompletion:^(NSError *_Nullable error) {
  195. if (error) {
  196. [self logFailure:@"delete account failed" error:error];
  197. }
  198. [self showTypicalUIForUserUpdateResultsWithTitle:@"Delete User" error:error];
  199. }];
  200. }
  201. - (void)updatePhoneNumber:(NSString *_Nullable)phoneNumber
  202. completion:(nullable TestAutomationCallback)completion {
  203. [self showSpinner:^{
  204. [[AppManager phoneAuthProvider] verifyPhoneNumber:phoneNumber
  205. UIDelegate:nil
  206. completion:^(NSString *_Nullable verificationID,
  207. NSError *_Nullable error) {
  208. if (error) {
  209. [self logFailure:@"failed to send verification code" error:error];
  210. [self showMessagePrompt:error.localizedDescription];
  211. if (completion) {
  212. completion(error);
  213. }
  214. return;
  215. }
  216. [self logSuccess:@"Code sent"];
  217. [self showTextInputPromptWithMessage:@"Verification code:"
  218. keyboardType:UIKeyboardTypeNumberPad
  219. completionBlock:^(BOOL userPressedOK,
  220. NSString *_Nullable verificationCode) {
  221. if (!userPressedOK || !verificationCode.length) {
  222. return;
  223. }
  224. [self showSpinner:^{
  225. FIRPhoneAuthCredential *credential =
  226. [[AppManager phoneAuthProvider] credentialWithVerificationID:verificationID
  227. verificationCode:verificationCode];
  228. [[self user] updatePhoneNumberCredential:credential
  229. completion:^(NSError *_Nullable error) {
  230. if (error) {
  231. [self logFailure:@"update phone number failed" error:error];
  232. [self showMessagePrompt:error.localizedDescription];
  233. if (completion) {
  234. completion(error);
  235. }
  236. } else {
  237. [self logSuccess:@"update phone number succeeded."];
  238. if (completion) {
  239. completion(nil);
  240. }
  241. }
  242. }];
  243. }];
  244. }];
  245. }];
  246. }];
  247. }
  248. - (void)updatePhoneNumber {
  249. [self showTextInputPromptWithMessage:@"Update Phone #:"
  250. keyboardType:UIKeyboardTypePhonePad
  251. completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
  252. if (!userPressedOK || !phoneNumber.length) {
  253. return;
  254. }
  255. [self updatePhoneNumber:phoneNumber completion:nil];
  256. }];
  257. }
  258. @end
  259. NS_ASSUME_NONNULL_END