MainViewController+Email.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "MainViewController+Internal.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @implementation MainViewController (Email)
  21. - (StaticContentTableViewSection *)emailAuthSection {
  22. __weak typeof(self) weakSelf = self;
  23. return [StaticContentTableViewSection sectionWithTitle:@"Email Auth" cells:@[
  24. [StaticContentTableViewCell cellWithTitle:@"Create User"
  25. action:^{ [weakSelf createUser]; }],
  26. [StaticContentTableViewCell cellWithTitle:@"Sign in with Email Password"
  27. action:^{ [weakSelf signInEmailPassword]; }],
  28. [StaticContentTableViewCell cellWithTitle:@"Link with Email Password"
  29. action:^{ [weakSelf linkWithEmailPassword]; }],
  30. [StaticContentTableViewCell cellWithTitle:@"Unlink from Email Password"
  31. action:^{ [weakSelf unlinkFromProvider:FIREmailAuthProviderID completion:nil]; }],
  32. [StaticContentTableViewCell cellWithTitle:@"Reauthenticate Email Password"
  33. action:^{ [weakSelf reauthenticateEmailPassword]; }],
  34. [StaticContentTableViewCell cellWithTitle:@"Sign in with Email Link"
  35. action:^{ [weakSelf sendEmailSignInLink]; }],
  36. [StaticContentTableViewCell cellWithTitle:@"Send Email Sign in Link"
  37. action:^{ [weakSelf signInWithEmailLink]; }],
  38. ]];
  39. }
  40. - (void)createUser {
  41. [self showTextInputPromptWithMessage:@"Email:"
  42. keyboardType:UIKeyboardTypeEmailAddress
  43. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  44. if (!userPressedOK || !email.length) {
  45. return;
  46. }
  47. [self showTextInputPromptWithMessage:@"Password:"
  48. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  49. if (!userPressedOK) {
  50. return;
  51. }
  52. [self showSpinner:^{
  53. [[AppManager auth] createUserWithEmail:email
  54. password:password
  55. completion:^(FIRAuthDataResult *_Nullable result,
  56. NSError *_Nullable error) {
  57. if (error) {
  58. [self logFailure:@"create user failed" error:error];
  59. } else {
  60. [self logSuccess:@"create user succeeded."];
  61. [self log:result.user.uid];
  62. }
  63. [self hideSpinner:^{
  64. [self showTypicalUIForUserUpdateResultsWithTitle:@"Create User" error:error];
  65. }];
  66. }];
  67. }];
  68. }];
  69. }];
  70. }
  71. - (void)signUpNewEmail:(NSString *)email
  72. password:(NSString *)password
  73. callback:(nullable FIRAuthResultCallback)callback {
  74. [[AppManager auth] createUserWithEmail:email
  75. password:password
  76. completion:^(FIRAuthDataResult *_Nullable result,
  77. NSError *_Nullable error) {
  78. if (error) {
  79. [self logFailure:@"sign-up with Email/Password failed" error:error];
  80. if (callback) {
  81. callback(nil, error);
  82. }
  83. } else {
  84. [self logSuccess:@"sign-up with Email/Password succeeded."];
  85. if (callback) {
  86. callback(result.user, nil);
  87. }
  88. }
  89. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
  90. }];
  91. }
  92. - (void)signInEmailPassword {
  93. [self showTextInputPromptWithMessage:@"Email Address:"
  94. keyboardType:UIKeyboardTypeEmailAddress
  95. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  96. if (!userPressedOK || !email.length) {
  97. return;
  98. }
  99. [self showTextInputPromptWithMessage:@"Password:"
  100. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  101. if (!userPressedOK) {
  102. return;
  103. }
  104. [self showSpinner:^{
  105. [[AppManager auth] signInWithEmail:email
  106. password:password
  107. completion:^(FIRAuthDataResult *_Nullable authResult,
  108. NSError *_Nullable error) {
  109. [self hideSpinner:^{
  110. if (error) {
  111. [self logFailure:@"sign-in with Email/Password failed" error:error];
  112. } else {
  113. [self logSuccess:@"sign-in with Email/Password succeeded."];
  114. [self log:[NSString stringWithFormat:@"UID: %@",authResult.user.uid]];
  115. }
  116. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  117. }];
  118. }];
  119. }];
  120. }];
  121. }];
  122. }
  123. - (void)linkWithEmailPassword {
  124. [self showEmailPasswordDialogWithCompletion:^(FIRAuthCredential *credential) {
  125. [self showSpinner:^{
  126. [[self user] linkWithCredential:credential
  127. completion:^(FIRAuthDataResult *result, NSError *error) {
  128. if (error) {
  129. [self logFailure:@"link Email Password failed." error:error];
  130. } else {
  131. [self logSuccess:@"link Email Password succeeded."];
  132. }
  133. [self hideSpinner:^{
  134. [self showTypicalUIForUserUpdateResultsWithTitle:@"Link with Email Password" error:error];
  135. }];
  136. }];
  137. }];
  138. }];
  139. }
  140. - (void)reauthenticateEmailPassword {
  141. FIRUser *user = [self user];
  142. if (!user) {
  143. NSString *title = @"Missing User";
  144. NSString *message = @"There is no signed-in email/password user.";
  145. [self showMessagePromptWithTitle:title message:message showCancelButton:NO completion:nil];
  146. return;
  147. }
  148. [self showEmailPasswordDialogWithCompletion:^(FIRAuthCredential *credential) {
  149. [self showSpinner:^{
  150. [[self user] reauthenticateWithCredential:credential
  151. completion:^(FIRAuthDataResult *_Nullable result,
  152. NSError *_Nullable error) {
  153. if (error) {
  154. [self logFailure:@"reauthicate with email password failed." error:error];
  155. } else {
  156. [self logSuccess:@"reauthicate with email password succeeded."];
  157. }
  158. [self hideSpinner:^{
  159. [self showTypicalUIForUserUpdateResultsWithTitle:@"Reauthenticate Email Password" error:error];
  160. }];
  161. }];
  162. }];
  163. }];
  164. }
  165. - (void)showEmailPasswordDialogWithCompletion:(ShowEmailPasswordDialogCompletion)completion {
  166. [self showTextInputPromptWithMessage:@"Email Address:"
  167. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  168. if (!userPressedOK || !email.length) {
  169. return;
  170. }
  171. [self showTextInputPromptWithMessage:@"Password:"
  172. completionBlock:^(BOOL userPressedOK, NSString *_Nullable password) {
  173. if (!userPressedOK || !password.length) {
  174. return;
  175. }
  176. FIRAuthCredential *credential = [FIREmailAuthProvider credentialWithEmail:email
  177. password:password];
  178. completion(credential);
  179. }];
  180. }];
  181. }
  182. - (void)signInWithEmailLink {
  183. [self showTextInputPromptWithMessage:@"Email Address:"
  184. keyboardType:UIKeyboardTypeEmailAddress
  185. completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
  186. if (!userPressedOK || !email.length) {
  187. return;
  188. }
  189. [self showTextInputPromptWithMessage:@"Email Sign-In Link:"
  190. completionBlock:^(BOOL userPressedOK, NSString *_Nullable link) {
  191. if (!userPressedOK) {
  192. return;
  193. }
  194. if ([[FIRAuth auth] isSignInWithEmailLink:link]) {
  195. [self showSpinner:^{
  196. [[AppManager auth] signInWithEmail:email
  197. link:link
  198. completion:^(FIRAuthDataResult *_Nullable authResult,
  199. NSError *_Nullable error) {
  200. [self hideSpinner:^{
  201. if (error) {
  202. [self logFailure:@"sign-in with Email/Sign-In failed" error:error];
  203. } else {
  204. [self logSuccess:@"sign-in with Email/Sign-In link succeeded."];
  205. [self log:[NSString stringWithFormat:@"UID: %@",authResult.user.uid]];
  206. }
  207. [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
  208. }];
  209. }];
  210. }];
  211. } else {
  212. [self log:@"The sign-in link is invalid"];
  213. }
  214. }];
  215. }];
  216. }
  217. - (void)sendEmailSignInLink {
  218. [self showTextInputPromptWithMessage:@"Email:"
  219. completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
  220. if (!userPressedOK) {
  221. return;
  222. }
  223. [self showSpinner:^{
  224. void (^requestEmailSignInLink)(void (^)(NSError *)) = ^(void (^completion)(NSError *)) {
  225. [[AppManager auth] sendSignInLinkToEmail:userInput
  226. actionCodeSettings:[self actionCodeSettings]
  227. completion:completion];
  228. };
  229. requestEmailSignInLink(^(NSError *_Nullable error) {
  230. [self hideSpinner:^{
  231. if (error) {
  232. [self logFailure:@"Email Link request failed" error:error];
  233. [self showMessagePrompt:error.localizedDescription];
  234. return;
  235. }
  236. [self logSuccess:@"Email Link request succeeded."];
  237. [self showMessagePrompt:@"Sent"];
  238. }];
  239. });
  240. }];
  241. }];
  242. }
  243. @end
  244. NS_ASSUME_NONNULL_END