MainViewController+Email.m 12 KB

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