MainViewController+Email.m 11 KB

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