MainViewController+Email.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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+Email.h"
  17. #import "AppManager.h"
  18. #import "FIRMultiFactorResolver+Internal.h"
  19. #import "FIRMultiFactorSession+Internal.h"
  20. #import "FIRPhoneMultiFactorInfo.h"
  21. #import "MainViewController+Internal.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. extern NSString *const FIRAuthErrorUserInfoMultiFactorResolverKey;
  24. typedef void (^ShowEmailDialogCompletion)(FIRAuthCredential *credential);
  25. @implementation MainViewController (Email)
  26. - (StaticContentTableViewSection *)emailAuthSection {
  27. __weak typeof(self) weakSelf = self;
  28. return [StaticContentTableViewSection sectionWithTitle:@"Email Auth" cells:@[
  29. [StaticContentTableViewCell cellWithTitle:@"Create User"
  30. action:^{ [weakSelf createUser]; }],
  31. [StaticContentTableViewCell cellWithTitle:@"Sign in with Email Password"
  32. action:^{ [weakSelf signInEmailPassword]; }],
  33. [StaticContentTableViewCell cellWithTitle:@"Link with Email Password"
  34. action:^{ [weakSelf linkWithEmailPassword]; }],
  35. [StaticContentTableViewCell cellWithTitle:@"Reauthenticate Email Password"
  36. action:^{ [weakSelf reauthenticateEmailPassword]; }],
  37. [StaticContentTableViewCell cellWithTitle:@"Send Email Sign in Link"
  38. action:^{ [weakSelf sendEmailSignInLink]; }],
  39. [StaticContentTableViewCell cellWithTitle:@"Sign in with Email Link"
  40. action:^{ [weakSelf signInWithEmailLink]; }],
  41. [StaticContentTableViewCell cellWithTitle:@"Link with Email Link"
  42. action:^{ [weakSelf linkWithEmailLink]; }],
  43. [StaticContentTableViewCell cellWithTitle:@"Unlink from Email"
  44. action:^{ [weakSelf unlinkFromProvider:FIREmailAuthProviderID
  45. completion:nil]; }],
  46. ]];
  47. }
  48. - (void)createUser {
  49. [self showTextInputPromptWithMessage:@"Email:"
  50. keyboardType:UIKeyboardTypeEmailAddress
  51. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  52. if (!userPressedOK || !email.length) {
  53. return;
  54. }
  55. [self showTextInputPromptWithMessage:@"Password:"
  56. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  57. if (!userPressedOK) {
  58. return;
  59. }
  60. [self showSpinner:^{
  61. [[AppManager auth] createUserWithEmail:email
  62. password:password
  63. completion:^(FIRAuthDataResult *_Nullable result,
  64. NSError *_Nullable error) {
  65. if (error) {
  66. [self logFailure:@"create user failed" error:error];
  67. } else {
  68. [self logSuccess:@"create user succeeded."];
  69. [self log:result.user.uid];
  70. }
  71. [self hideSpinner:^{
  72. [self showTypicalUIForUserUpdateResultsWithTitle:@"Create User" error:error];
  73. }];
  74. }];
  75. }];
  76. }];
  77. }];
  78. }
  79. - (void)signUpNewEmail:(NSString *)email
  80. password:(NSString *)password
  81. callback:(nullable FIRAuthResultCallback)callback {
  82. [[AppManager auth] createUserWithEmail:email
  83. password:password
  84. completion:^(FIRAuthDataResult *_Nullable result,
  85. NSError *_Nullable error) {
  86. if (error) {
  87. [self logFailure:@"sign-up with Email/Password failed" error:error];
  88. if (callback) {
  89. callback(nil, error);
  90. }
  91. } else {
  92. [self logSuccess:@"sign-up with Email/Password succeeded."];
  93. if (callback) {
  94. callback(result.user, nil);
  95. }
  96. }
  97. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
  98. }];
  99. }
  100. - (void)signInEmailPassword {
  101. [self showTextInputPromptWithMessage:@"Email Address"
  102. keyboardType:UIKeyboardTypeEmailAddress
  103. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  104. if (!userPressedOK || !email.length) {
  105. return;
  106. }
  107. [self showTextInputPromptWithMessage:@"Password"
  108. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  109. if (!userPressedOK) {
  110. return;
  111. }
  112. [self showSpinner:^{
  113. [[AppManager auth] signInWithEmail:email
  114. password:password
  115. completion:^(FIRAuthDataResult *_Nullable authResult,
  116. NSError *_Nullable error) {
  117. [self hideSpinner:^{
  118. if (error) {
  119. if (error.code == FIRAuthErrorCodeSecondFactorRequired) {
  120. FIRMultiFactorResolver *resolver = error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey];
  121. NSMutableString *displayNameString = [NSMutableString string];
  122. for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) {
  123. [displayNameString appendString:tmpFactorInfo.displayName];
  124. [displayNameString appendString:@" "];
  125. }
  126. [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Select factor to sign in\n%@", displayNameString]
  127. completionBlock:^(BOOL userPressedOK, NSString *_Nullable displayName) {
  128. FIRPhoneMultiFactorInfo* selectedHint;
  129. for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) {
  130. if ([displayName isEqualToString:tmpFactorInfo.displayName]) {
  131. selectedHint = (FIRPhoneMultiFactorInfo *)tmpFactorInfo;
  132. }
  133. }
  134. [FIRPhoneAuthProvider.provider
  135. verifyPhoneNumberWithMultiFactorInfo:selectedHint
  136. UIDelegate:nil
  137. multiFactorSession:resolver.session
  138. completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
  139. if (error) {
  140. [self logFailure:@"Multi factor start sign in failed." error:error];
  141. } else {
  142. [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Verification code for %@", selectedHint.displayName]
  143. completionBlock:^(BOOL userPressedOK, NSString *_Nullable verificationCode) {
  144. FIRPhoneAuthCredential *credential =
  145. [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID
  146. verificationCode:verificationCode];
  147. FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
  148. [resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
  149. if (error) {
  150. [self logFailure:@"Multi factor finalize sign in failed." error:error];
  151. } else {
  152. [self logSuccess:@"Multi factor finalize sign in succeeded."];
  153. }
  154. }];
  155. }];
  156. }
  157. }];
  158. }];
  159. } else {
  160. [self logFailure:@"sign-in with Email/Password failed" error:error];
  161. }
  162. } else {
  163. [self logSuccess:@"sign-in with Email/Password succeeded."];
  164. [self log:[NSString stringWithFormat:@"UID: %@",authResult.user.uid]];
  165. }
  166. }];
  167. }];
  168. }];
  169. }];
  170. }];
  171. }
  172. - (void)linkWithEmailPassword {
  173. [self showEmailPasswordDialogWithCompletion:^(FIRAuthCredential *credential) {
  174. [self showSpinner:^{
  175. [[self user] linkWithCredential:credential
  176. completion:^(FIRAuthDataResult *result, NSError *error) {
  177. if (error) {
  178. [self logFailure:@"link Email Password failed." error:error];
  179. } else {
  180. [self logSuccess:@"link Email Password succeeded."];
  181. }
  182. [self hideSpinner:^{
  183. [self showTypicalUIForUserUpdateResultsWithTitle:@"Link with Email Password" error:error];
  184. }];
  185. }];
  186. }];
  187. }];
  188. }
  189. - (void)reauthenticateEmailPassword {
  190. FIRUser *user = [self user];
  191. if (!user) {
  192. NSString *title = @"Missing User";
  193. NSString *message = @"There is no signed-in email/password user.";
  194. [self showMessagePromptWithTitle:title message:message showCancelButton:NO completion:nil];
  195. return;
  196. }
  197. [self showEmailPasswordDialogWithCompletion:^(FIRAuthCredential *credential) {
  198. [self showSpinner:^{
  199. [[self user] reauthenticateWithCredential:credential
  200. completion:^(FIRAuthDataResult *_Nullable result,
  201. NSError *_Nullable error) {
  202. if (error) {
  203. [self logFailure:@"reauthicate with email password failed." error:error];
  204. } else {
  205. [self logSuccess:@"reauthicate with email password succeeded."];
  206. }
  207. [self hideSpinner:^{
  208. [self showTypicalUIForUserUpdateResultsWithTitle:@"Reauthenticate Email Password" error:error];
  209. }];
  210. }];
  211. }];
  212. }];
  213. }
  214. - (void)signInWithEmailLink {
  215. [self showTextInputPromptWithMessage:@"Email Address:"
  216. keyboardType:UIKeyboardTypeEmailAddress
  217. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  218. if (!userPressedOK || !email.length) {
  219. return;
  220. }
  221. [self showTextInputPromptWithMessage:@"Email Sign-In Link:"
  222. completionBlock:^(BOOL userPressedOK, NSString *_Nullable link) {
  223. if (!userPressedOK) {
  224. return;
  225. }
  226. if ([[FIRAuth auth] isSignInWithEmailLink:link]) {
  227. [self showSpinner:^{
  228. [[AppManager auth] signInWithEmail:email
  229. link:link
  230. completion:^(FIRAuthDataResult *_Nullable authResult,
  231. NSError *_Nullable error) {
  232. [self hideSpinner:^{
  233. if (error) {
  234. [self logFailure:@"sign-in with Email/Sign-In failed" error:error];
  235. } else {
  236. [self logSuccess:@"sign-in with Email/Sign-In link succeeded."];
  237. [self log:[NSString stringWithFormat:@"UID: %@",authResult.user.uid]];
  238. }
  239. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  240. }];
  241. }];
  242. }];
  243. } else {
  244. [self log:@"The sign-in link is invalid"];
  245. }
  246. }];
  247. }];
  248. }
  249. - (void)sendEmailSignInLink {
  250. [self showTextInputPromptWithMessage:@"Email:"
  251. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  252. if (!userPressedOK) {
  253. return;
  254. }
  255. [self showSpinner:^{
  256. void (^requestEmailSignInLink)(void (^)(NSError *)) = ^(void (^completion)(NSError *)) {
  257. [[AppManager auth] sendSignInLinkToEmail:userInput
  258. actionCodeSettings:[self actionCodeSettings]
  259. completion:completion];
  260. };
  261. requestEmailSignInLink(^(NSError *_Nullable error) {
  262. [self hideSpinner:^{
  263. if (error) {
  264. [self logFailure:@"Email Link request failed" error:error];
  265. [self showMessagePrompt:error.localizedDescription];
  266. return;
  267. }
  268. [self logSuccess:@"Email Link request succeeded."];
  269. [self showMessagePrompt:@"Sent"];
  270. }];
  271. });
  272. }];
  273. }];
  274. }
  275. - (void)linkWithEmailLink {
  276. [self showEmailLinkDialogWithCompletion:^(FIRAuthCredential *credential) {
  277. [self showSpinner:^{
  278. [[self user] linkWithCredential:credential
  279. completion:^(FIRAuthDataResult *result, NSError *error) {
  280. if (error) {
  281. [self logFailure:@"link Email Link failed." error:error];
  282. } else {
  283. [self logSuccess:@"link Email Link succeeded."];
  284. }
  285. [self hideSpinner:^{
  286. [self showTypicalUIForUserUpdateResultsWithTitle:@"Link with Email Link" error:error];
  287. }];
  288. }];
  289. }];
  290. }];
  291. }
  292. - (void)showEmailPasswordDialogWithCompletion:(ShowEmailDialogCompletion)completion {
  293. [self showTextInputPromptWithMessage:@"Email Address:"
  294. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  295. if (!userPressedOK || !email.length) {
  296. return;
  297. }
  298. [self showTextInputPromptWithMessage:@"Password:"
  299. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  300. if (!userPressedOK || !password.length) {
  301. return;
  302. }
  303. FIRAuthCredential *credential = [FIREmailAuthProvider credentialWithEmail:email
  304. password:password];
  305. completion(credential);
  306. }];
  307. }];
  308. }
  309. - (void)showEmailLinkDialogWithCompletion:(ShowEmailDialogCompletion)completion {
  310. [self showTextInputPromptWithMessage:@"Email Address:"
  311. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  312. if (!userPressedOK || !email.length) {
  313. return;
  314. }
  315. [self showTextInputPromptWithMessage:@"Link:"
  316. completionBlock:^(BOOL userPressedOK, NSString *_Nullable link) {
  317. if (!userPressedOK || !link.length) {
  318. return;
  319. }
  320. FIRAuthCredential *credential = [FIREmailAuthProvider credentialWithEmail:email
  321. link:link];
  322. completion(credential);
  323. }];
  324. }];
  325. }
  326. @end
  327. NS_ASSUME_NONNULL_END