MainViewController+User.m 9.8 KB

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