MainViewController+MultiFactor.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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+MultiFactor.h"
  17. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  18. #import "FirebaseAuth/Sources/User/FIRUser_Internal.h"
  19. #import <FirebaseAuth/FIRMultiFactorInfo.h>
  20. #import <FirebaseAuth/FIRPhoneAuthProvider.h>
  21. #import "MainViewController+Internal.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. @implementation MainViewController (MultiFactor)
  24. - (StaticContentTableViewSection *)multiFactorSection {
  25. __weak typeof(self) weakSelf = self;
  26. return [StaticContentTableViewSection sectionWithTitle:@"Multi Factor" cells:@[
  27. [StaticContentTableViewCell cellWithTitle:@"Phone Enroll"
  28. action:^{ [weakSelf phoneEnroll]; }],
  29. [StaticContentTableViewCell cellWithTitle:@"Phone Unenroll"
  30. action:^{ [weakSelf phoneUnenroll]; }],
  31. ]];
  32. }
  33. - (void)phoneEnroll {
  34. FIRUser *user = FIRAuth.auth.currentUser;
  35. if (!user) {
  36. [self logFailure:@"Please sign in first." error:nil];
  37. return;
  38. }
  39. [self showTextInputPromptWithMessage:@"Phone Number"
  40. completionBlock:^(BOOL userPressedOK, NSString *_Nullable phoneNumber) {
  41. [user.multiFactor
  42. getSessionWithCompletion:^(FIRMultiFactorSession *_Nullable session, NSError *_Nullable error) {
  43. [FIRPhoneAuthProvider.provider verifyPhoneNumber:phoneNumber
  44. UIDelegate:nil
  45. multiFactorSession:session
  46. completion:^(NSString * _Nullable verificationID,
  47. NSError * _Nullable error) {
  48. if (error) {
  49. [self logFailure:@"Multi factor start enroll failed." error:error];
  50. } else {
  51. [self showTextInputPromptWithMessage:@"Verification code"
  52. completionBlock:^(BOOL userPressedOK,
  53. NSString *_Nullable verificationCode) {
  54. FIRPhoneAuthCredential *credential =
  55. [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID
  56. verificationCode:verificationCode];
  57. FIRMultiFactorAssertion *assertion =
  58. [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
  59. [self showTextInputPromptWithMessage:@"Display name"
  60. completionBlock:^(BOOL userPressedOK,
  61. NSString *_Nullable displayName) {
  62. [user.multiFactor enrollWithAssertion:assertion
  63. displayName:displayName
  64. completion:^(NSError *_Nullable error) {
  65. if (error) {
  66. [self logFailure:@"Multi factor finalize enroll failed." error:error];
  67. } else {
  68. [self logSuccess:@"Multi factor finalize enroll succeeded."];
  69. }
  70. }];
  71. }];
  72. }];
  73. }
  74. }];
  75. }];
  76. }];
  77. }
  78. - (void)phoneUnenroll {
  79. NSMutableString *displayNameString = [NSMutableString string];
  80. for (FIRMultiFactorInfo *tmpFactorInfo in FIRAuth.auth.currentUser.multiFactor.enrolledFactors) {
  81. [displayNameString appendString:tmpFactorInfo.displayName];
  82. [displayNameString appendString:@" "];
  83. }
  84. [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Multifactor Unenroll\n%@", displayNameString]
  85. completionBlock:^(BOOL userPressedOK, NSString *_Nullable displayName) {
  86. FIRMultiFactorInfo *factorInfo;
  87. for (FIRMultiFactorInfo *tmpFactorInfo in FIRAuth.auth.currentUser.multiFactor.enrolledFactors) {
  88. if ([displayName isEqualToString:tmpFactorInfo.displayName]) {
  89. factorInfo = tmpFactorInfo;
  90. }
  91. }
  92. [FIRAuth.auth.currentUser.multiFactor unenrollWithInfo:factorInfo
  93. completion:^(NSError * _Nullable error) {
  94. if (error) {
  95. [self logFailure:@"Multi factor unenroll failed." error:error];
  96. } else {
  97. [self logSuccess:@"Multi factor unenroll succeeded."];
  98. }
  99. }];
  100. }];
  101. }
  102. @end
  103. NS_ASSUME_NONNULL_END