MainViewController+User.m 11 KB

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