BGFirebaseSMS.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // BGFirebaseSMS.m
  3. // BuguLive
  4. //
  5. // Created by 志刚杨 on 2022/7/25.
  6. // Copyright © 2022 xfg. All rights reserved.
  7. //
  8. #import "BGFirebaseSMS.h"
  9. #import "UIViewController+Alerts.h"
  10. @import FirebaseCore;
  11. @import FirebaseFirestore;
  12. @import FirebaseAuth;
  13. @implementation BGFirebaseSMS
  14. -(void)sendSMS:(NSString *)phone block:(AppCommonBlock)block
  15. {
  16. [[FIRPhoneAuthProvider provider] verifyPhoneNumber:phone
  17. UIDelegate:nil
  18. completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
  19. if (error) {
  20. [BGHUDHelper alert:error.localizedDescription];
  21. return;
  22. }
  23. else{
  24. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  25. [defaults setObject:verificationID forKey:@"authVerificationID"];
  26. block(nil);
  27. }
  28. // Sign in using the verificationID and the code sent to the user
  29. // ...
  30. }];
  31. }
  32. -(void)verifyCode:(NSString *)code block:(AppCommonBlock)block
  33. {
  34. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  35. NSString *verificationID = [defaults stringForKey:@"authVerificationID"];
  36. FIRAuthCredential *credential = [[FIRPhoneAuthProvider provider]
  37. credentialWithVerificationID:verificationID
  38. verificationCode:code];
  39. [[FIRAuth auth] signInWithCredential:credential
  40. completion:^(FIRAuthDataResult * _Nullable authResult,
  41. NSError * _Nullable error) {
  42. if (error && error.code == FIRAuthErrorCodeSecondFactorRequired) {
  43. FIRMultiFactorResolver *resolver = error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey];
  44. NSMutableString *displayNameString = [NSMutableString string];
  45. for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) {
  46. [displayNameString appendString:tmpFactorInfo.displayName];
  47. [displayNameString appendString:@" "];
  48. }
  49. [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Select factor to sign in\n%@", displayNameString]
  50. completionBlock:^(BOOL userPressedOK, NSString *_Nullable displayName) {
  51. FIRPhoneMultiFactorInfo* selectedHint;
  52. for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) {
  53. if ([displayName isEqualToString:tmpFactorInfo.displayName]) {
  54. selectedHint = (FIRPhoneMultiFactorInfo *)tmpFactorInfo;
  55. }
  56. }
  57. [FIRPhoneAuthProvider.provider
  58. verifyPhoneNumberWithMultiFactorInfo:selectedHint
  59. UIDelegate:nil
  60. multiFactorSession:resolver.session
  61. completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
  62. if (error) {
  63. [BGHUDHelper alert:error.localizedDescription];
  64. } else {
  65. [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Verification code for %@", selectedHint.displayName]
  66. completionBlock:^(BOOL userPressedOK, NSString *_Nullable verificationCode) {
  67. FIRPhoneAuthCredential *credential =
  68. [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID
  69. verificationCode:verificationCode];
  70. FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
  71. [resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
  72. if (error) {
  73. [BGHUDHelper alert:error.localizedDescription];
  74. } else {
  75. NSLog(@"Multi factor finanlize sign in succeeded.");
  76. }
  77. }];
  78. }];
  79. }
  80. }];
  81. }];
  82. }
  83. else if (error) {
  84. [BGHUDHelper alert:error.localizedDescription];
  85. // ...
  86. return;
  87. }
  88. // User successfully signed in. Get user data from the FIRUser object
  89. if (authResult == nil) { return; }
  90. FIRUser *user = authResult.user;
  91. [user getIDTokenForcingRefresh:YES completion:^(NSString * _Nullable token, NSError * _Nullable error) {
  92. NSLog(@"google token %@...",token);
  93. AppBlockModel *model = [AppBlockModel new];
  94. model.retDict = @{@"token":token};
  95. block(model);
  96. }];
  97. }];
  98. }
  99. @end