MainViewController+OOB.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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+OOB.h"
  17. #import "AppManager.h"
  18. #import "MainViewController+Internal.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @implementation MainViewController (OOB)
  21. - (StaticContentTableViewSection *)oobSection {
  22. __weak typeof(self) weakSelf = self;
  23. return [StaticContentTableViewSection sectionWithTitle:@"OOB" cells:@[
  24. [StaticContentTableViewCell cellWithTitle:@"Action Type"
  25. value:[self actionCodeRequestTypeString]
  26. action:^{ [weakSelf toggleActionCodeRequestType]; }],
  27. [StaticContentTableViewCell cellWithTitle:@"Continue URL"
  28. value:self.actionCodeContinueURL.absoluteString ?: @"(nil)"
  29. action:^{ [weakSelf changeActionCodeContinueURL]; }],
  30. [StaticContentTableViewCell cellWithTitle:@"Request Verify Email"
  31. action:^{ [weakSelf requestVerifyEmail]; }],
  32. [StaticContentTableViewCell cellWithTitle:@"Request Password Reset"
  33. action:^{ [weakSelf requestPasswordReset]; }],
  34. [StaticContentTableViewCell cellWithTitle:@"Reset Password"
  35. action:^{ [weakSelf resetPassword]; }],
  36. [StaticContentTableViewCell cellWithTitle:@"Check Action Code"
  37. action:^{ [weakSelf checkActionCode]; }],
  38. [StaticContentTableViewCell cellWithTitle:@"Apply Action Code"
  39. action:^{ [weakSelf applyActionCode]; }],
  40. [StaticContentTableViewCell cellWithTitle:@"Verify Password Reset Code"
  41. action:^{ [weakSelf verifyPasswordResetCode]; }],
  42. ]];
  43. }
  44. - (void)toggleActionCodeRequestType {
  45. switch (self.actionCodeRequestType) {
  46. case ActionCodeRequestTypeInApp:
  47. self.actionCodeRequestType = ActionCodeRequestTypeContinue;
  48. break;
  49. case ActionCodeRequestTypeContinue:
  50. self.actionCodeRequestType = ActionCodeRequestTypeEmail;
  51. break;
  52. case ActionCodeRequestTypeEmail:
  53. self.actionCodeRequestType = ActionCodeRequestTypeInApp;
  54. break;
  55. }
  56. [self updateTable];
  57. }
  58. - (NSString *)nameForActionCodeOperation:(FIRActionCodeOperation)operation {
  59. switch (operation) {
  60. case FIRActionCodeOperationVerifyEmail:
  61. return @"Verify Email";
  62. case FIRActionCodeOperationRecoverEmail:
  63. return @"Recover Email";
  64. case FIRActionCodeOperationPasswordReset:
  65. return @"Password Reset";
  66. case FIRActionCodeOperationEmailLink:
  67. return @"Email Sign-In Link";
  68. case FIRActionCodeOperationUnknown:
  69. return @"Unknown action";
  70. }
  71. }
  72. - (NSString *)actionCodeRequestTypeString {
  73. switch (self.actionCodeRequestType) {
  74. case ActionCodeRequestTypeInApp:
  75. return @"In-App + Continue URL";
  76. case ActionCodeRequestTypeContinue:
  77. return @"Continue URL";
  78. case ActionCodeRequestTypeEmail:
  79. return @"Email Only";
  80. }
  81. }
  82. - (void)changeActionCodeContinueURL {
  83. [self showTextInputPromptWithMessage:@"Continue URL"
  84. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  85. if (userPressedOK) {
  86. self.actionCodeContinueURL = userInput.length ? [NSURL URLWithString:userInput] : nil;
  87. [self updateTable];
  88. }
  89. }];
  90. }
  91. - (void)requestVerifyEmail {
  92. [self showSpinner:^{
  93. void (^sendEmailVerification)(void (^)(NSError *)) = ^(void (^completion)(NSError *)) {
  94. if (self.actionCodeRequestType == ActionCodeRequestTypeEmail) {
  95. [[self user] sendEmailVerificationWithCompletion:completion];
  96. } else {
  97. [[self user] sendEmailVerificationWithActionCodeSettings:[self actionCodeSettings]
  98. completion:completion];
  99. }
  100. };
  101. sendEmailVerification(^(NSError *_Nullable error) {
  102. [self hideSpinner:^{
  103. if (error) {
  104. [self logFailure:@"request verify email failed" error:error];
  105. [self showMessagePrompt:error.localizedDescription];
  106. return;
  107. }
  108. [self logSuccess:@"request verify email succeeded."];
  109. [self showMessagePrompt:@"Sent"];
  110. }];
  111. });
  112. }];
  113. }
  114. - (void)requestPasswordReset {
  115. [self showTextInputPromptWithMessage:@"Email:"
  116. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  117. if (!userPressedOK || !userInput.length) {
  118. return;
  119. }
  120. [self showSpinner:^{
  121. void (^requestPasswordReset)(void (^)(NSError *)) = ^(void (^completion)(NSError *)) {
  122. if (self.actionCodeRequestType == ActionCodeRequestTypeEmail) {
  123. [[AppManager auth] sendPasswordResetWithEmail:userInput completion:completion];
  124. } else {
  125. [[AppManager auth] sendPasswordResetWithEmail:userInput
  126. actionCodeSettings:[self actionCodeSettings]
  127. completion:completion];
  128. }
  129. };
  130. requestPasswordReset(^(NSError *_Nullable error) {
  131. [self hideSpinner:^{
  132. if (error) {
  133. [self logFailure:@"request password reset failed" error:error];
  134. [self showMessagePrompt:error.localizedDescription];
  135. return;
  136. }
  137. [self logSuccess:@"request password reset succeeded."];
  138. [self showMessagePrompt:@"Sent"];
  139. }];
  140. });
  141. }];
  142. }];
  143. }
  144. - (void)resetPassword {
  145. [self showTextInputPromptWithMessage:@"OOB Code:"
  146. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  147. if (!userPressedOK || !userInput.length) {
  148. return;
  149. }
  150. NSString *code = userInput;
  151. [self showTextInputPromptWithMessage:@"New Password:"
  152. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  153. if (!userPressedOK || !userInput.length) {
  154. return;
  155. }
  156. [self showSpinner:^{
  157. [[AppManager auth] confirmPasswordResetWithCode:code
  158. newPassword:userInput
  159. completion:^(NSError *_Nullable error) {
  160. [self hideSpinner:^{
  161. if (error) {
  162. [self logFailure:@"Password reset failed" error:error];
  163. [self showMessagePrompt:error.localizedDescription];
  164. return;
  165. }
  166. [self logSuccess:@"Password reset succeeded."];
  167. [self showMessagePrompt:@"Password reset succeeded."];
  168. }];
  169. }];
  170. }];
  171. }];
  172. }];
  173. }
  174. - (void)checkActionCode {
  175. [self showTextInputPromptWithMessage:@"OOB Code:"
  176. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  177. if (!userPressedOK || !userInput.length) {
  178. return;
  179. }
  180. [self showSpinner:^{
  181. [[AppManager auth] checkActionCode:userInput completion:^(FIRActionCodeInfo *_Nullable info,
  182. NSError *_Nullable error) {
  183. [self hideSpinner:^{
  184. if (error) {
  185. [self logFailure:@"Check action code failed" error:error];
  186. [self showMessagePrompt:error.localizedDescription];
  187. return;
  188. }
  189. [self logSuccess:@"Check action code succeeded."];
  190. NSString *email = [info dataForKey:FIRActionCodeEmailKey];
  191. NSString *fromEmail = [info dataForKey:FIRActionCodeFromEmailKey];
  192. NSString *message =
  193. fromEmail ? [NSString stringWithFormat:@"%@ -> %@", fromEmail, email] : email;
  194. NSString *operation = [self nameForActionCodeOperation:info.operation];
  195. [self showMessagePromptWithTitle:operation
  196. message:message
  197. showCancelButton:NO
  198. completion:nil];
  199. }];
  200. }];
  201. }];
  202. }];
  203. }
  204. - (void)applyActionCode {
  205. [self showTextInputPromptWithMessage:@"OOB Code:"
  206. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  207. if (!userPressedOK || !userInput.length) {
  208. return;
  209. }
  210. [self showSpinner:^{
  211. [[AppManager auth] applyActionCode:userInput completion:^(NSError *_Nullable error) {
  212. [self hideSpinner:^{
  213. if (error) {
  214. [self logFailure:@"Apply action code failed" error:error];
  215. [self showMessagePrompt:error.localizedDescription];
  216. return;
  217. }
  218. [self logSuccess:@"Apply action code succeeded."];
  219. [self showMessagePrompt:@"Action code was properly applied."];
  220. }];
  221. }];
  222. }];
  223. }];
  224. }
  225. - (void)verifyPasswordResetCode {
  226. [self showTextInputPromptWithMessage:@"OOB Code:"
  227. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  228. if (!userPressedOK || !userInput.length) {
  229. return;
  230. }
  231. [self showSpinner:^{
  232. [[AppManager auth] verifyPasswordResetCode:userInput completion:^(NSString *_Nullable email,
  233. NSError *_Nullable error) {
  234. [self hideSpinner:^{
  235. if (error) {
  236. [self logFailure:@"Verify password reset code failed" error:error];
  237. [self showMessagePrompt:error.localizedDescription];
  238. return;
  239. }
  240. [self logSuccess:@"Verify password resest code succeeded."];
  241. NSString *alertMessage =
  242. [[NSString alloc] initWithFormat:@"Code verified for email: %@", email];
  243. [self showMessagePrompt:alertMessage];
  244. }];
  245. }];
  246. }];
  247. }];
  248. }
  249. @end
  250. NS_ASSUME_NONNULL_END