PhoneAuthProvider+Combine.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if os(iOS)
  15. import Foundation
  16. import Combine
  17. import FirebaseAuth
  18. @available(iOS 13.0, *)
  19. @available(macOS, unavailable)
  20. @available(macCatalyst, unavailable)
  21. @available(tvOS, unavailable)
  22. @available(watchOS, unavailable)
  23. public extension PhoneAuthProvider {
  24. /// Starts the phone number authentication flow by sending a verification code to the
  25. /// specified phone number.
  26. ///
  27. /// The publisher will emit events on the **main** thread.
  28. ///
  29. /// - Parameters:
  30. /// - phoneNumber: The phone number to be verified.
  31. /// - uiDelegate: An object used to present the `SFSafariViewController`. The object is
  32. /// retained by this method until the completion block is executed.
  33. ///
  34. /// - Returns: A publisher that emits an `VerificationID` when the verification flow completed
  35. /// successfully, or an error otherwise. The publisher will emit on the *main* thread.
  36. ///
  37. /// - Remark:
  38. /// Possible error codes:
  39. ///
  40. /// - `FIRAuthErrorCodeCaptchaCheckFailed` - Indicates that the reCAPTCHA token obtained by
  41. /// the Firebase Auth is invalid or has expired.
  42. /// - `FIRAuthErrorCodeQuotaExceeded` - Indicates that the phone verification quota for this
  43. /// project has been exceeded.
  44. /// - `FIRAuthErrorCodeInvalidPhoneNumber` - Indicates that the phone number provided is
  45. /// invalid.
  46. /// - `FIRAuthErrorCodeMissingPhoneNumber` - Indicates that a phone number was not provided.
  47. @discardableResult
  48. func verifyPhoneNumber(_ phoneNumber: String,
  49. uiDelegate: AuthUIDelegate? = nil)
  50. -> Future<String, Error> {
  51. Future<String, Error> { promise in
  52. self.verifyPhoneNumber(phoneNumber, uiDelegate: uiDelegate) { verificationID, error in
  53. if let error {
  54. promise(.failure(error))
  55. } else if let verificationID {
  56. promise(.success(verificationID))
  57. }
  58. }
  59. }
  60. }
  61. /// Verify ownership of the second factor phone number by the current user.
  62. ///
  63. /// The publisher will emit events on the **main** thread.
  64. ///
  65. /// - Parameters:
  66. /// - phoneNumber: The phone number to be verified.
  67. /// - uiDelegate: An object used to present the `SFSafariViewController`. The object is
  68. /// retained by this method until the completion block is executed.
  69. /// - session: A session to identify the MFA flow. For enrollment, this identifies the
  70. /// user trying to enroll. For sign-in, this identifies that the user already passed the
  71. /// first factor challenge.
  72. ///
  73. /// - Returns: A publisher that emits an `VerificationID` when the verification flow completed
  74. /// successfully, or an error otherwise. The publisher will emit on the *main* thread.
  75. ///
  76. /// - Remark:
  77. /// Possible error codes:
  78. ///
  79. /// - `FIRAuthErrorCodeCaptchaCheckFailed` - Indicates that the reCAPTCHA token obtained by
  80. /// the Firebase Auth is invalid or has expired.
  81. /// - `FIRAuthErrorCodeQuotaExceeded` - Indicates that the phone verification quota for this
  82. /// project has been exceeded.
  83. /// - `FIRAuthErrorCodeInvalidPhoneNumber` - Indicates that the phone number provided is
  84. /// invalid.
  85. /// - `FIRAuthErrorCodeMissingPhoneNumber` - Indicates that a phone number was not provided.
  86. @discardableResult
  87. func verifyPhoneNumber(_ phoneNumber: String,
  88. uiDelegate: AuthUIDelegate? = nil,
  89. multiFactorSession: MultiFactorSession?)
  90. -> Future<String, Error> {
  91. Future<String, Error> { promise in
  92. self.verifyPhoneNumber(
  93. phoneNumber,
  94. uiDelegate: uiDelegate,
  95. multiFactorSession: multiFactorSession
  96. ) { verificationID, error in
  97. if let error {
  98. promise(.failure(error))
  99. } else if let verificationID {
  100. promise(.success(verificationID))
  101. }
  102. }
  103. }
  104. }
  105. /// Verify ownership of the second factor phone number by the current user.
  106. ///
  107. /// The publisher will emit events on the **main** thread.
  108. ///
  109. /// - Parameters:
  110. /// - phoneNumber: The phone number to be verified.
  111. /// - UIDelegate: An object used to present the SFSafariViewController. The object is
  112. /// retained by this method until the completion block is executed.
  113. /// - multiFactorSession: session A session to identify the MFA flow. For enrollment, this
  114. /// identifies the user trying to enroll. For sign-in, this identifies that the user
  115. /// already
  116. /// passed the first factor challenge.
  117. /// - Returns: A publisher that emits an `VerificationID` when the verification flow completed
  118. /// successfully, or an error otherwise. The publisher will emit on the *main* thread.
  119. @discardableResult
  120. func verifyPhoneNumber(with phoneMultiFactorInfo: PhoneMultiFactorInfo,
  121. uiDelegate: AuthUIDelegate? = nil,
  122. multiFactorSession: MultiFactorSession?)
  123. -> Future<String, Error> {
  124. Future<String, Error> { promise in
  125. self.verifyPhoneNumber(with: phoneMultiFactorInfo,
  126. uiDelegate: uiDelegate,
  127. multiFactorSession: multiFactorSession) { verificationID, error in
  128. if let error {
  129. promise(.failure(error))
  130. } else if let verificationID {
  131. promise(.success(verificationID))
  132. }
  133. }
  134. }
  135. }
  136. }
  137. #endif // os(iOS)