PhoneAuthProvider+Combine.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 canImport(Combine) && swift(>=5.0)
  15. #if os(iOS)
  16. import Foundation
  17. import Combine
  18. import FirebaseAuth
  19. @available(swift 5.0)
  20. @available(iOS 13.0, *)
  21. @available(macOS, unavailable)
  22. @available(macCatalyst, unavailable)
  23. @available(tvOS, unavailable)
  24. @available(watchOS, unavailable)
  25. public extension PhoneAuthProvider {
  26. /// Starts the phone number authentication flow by sending a verification code to the
  27. /// specified phone number.
  28. ///
  29. /// The publisher will emit events on the **main** thread.
  30. ///
  31. /// - Parameters:
  32. /// - phoneNumber: The phone number to be verified.
  33. /// - uiDelegate: An object used to present the `SFSafariViewController`. The object is retained
  34. /// by this method until the completion block is executed.
  35. ///
  36. /// - Returns: A publisher that emits an `VerificationID` when the verification flow completed
  37. /// successfully, or an error otherwise. The publisher will emit on the *main* thread.
  38. ///
  39. /// - Remark:
  40. /// Possible error codes:
  41. ///
  42. /// - `FIRAuthErrorCodeCaptchaCheckFailed` - Indicates that the reCAPTCHA token obtained by
  43. /// the Firebase Auth is invalid or has expired.
  44. /// - `FIRAuthErrorCodeQuotaExceeded` - Indicates that the phone verification quota for this
  45. /// project has been exceeded.
  46. /// - `FIRAuthErrorCodeInvalidPhoneNumber` - Indicates that the phone number provided is
  47. /// invalid.
  48. /// - `FIRAuthErrorCodeMissingPhoneNumber` - Indicates that a phone number was not provided.
  49. @discardableResult
  50. func verifyPhoneNumber(_ phoneNumber: String,
  51. uiDelegate: AuthUIDelegate? = nil)
  52. -> Future<String, Error> {
  53. Future<String, Error> { promise in
  54. self.verifyPhoneNumber(phoneNumber, uiDelegate: uiDelegate) { verificationID, error in
  55. if let error = error {
  56. promise(.failure(error))
  57. } else if let verificationID = verificationID {
  58. promise(.success(verificationID))
  59. }
  60. }
  61. }
  62. }
  63. /// Verify ownership of the second factor phone number by the current user.
  64. ///
  65. /// The publisher will emit events on the **main** thread.
  66. ///
  67. /// - Parameters:
  68. /// - phoneNumber: The phone number to be verified.
  69. /// - uiDelegate: An object used to present the `SFSafariViewController`. The object is retained
  70. /// by this method until the completion block is executed.
  71. /// - session: A session to identify the MFA flow. For enrollment, this identifies the user
  72. /// trying to enroll. For sign-in, this identifies that the user already passed the first
  73. /// factor challenge.
  74. ///
  75. /// - Returns: A publisher that emits an `VerificationID` when the verification flow completed
  76. /// successfully, or an error otherwise. The publisher will emit on the *main* thread.
  77. ///
  78. /// - Remark:
  79. /// Possible error codes:
  80. ///
  81. /// - `FIRAuthErrorCodeCaptchaCheckFailed` - Indicates that the reCAPTCHA token obtained by
  82. /// the Firebase Auth is invalid or has expired.
  83. /// - `FIRAuthErrorCodeQuotaExceeded` - Indicates that the phone verification quota for this
  84. /// project has been exceeded.
  85. /// - `FIRAuthErrorCodeInvalidPhoneNumber` - Indicates that the phone number provided is
  86. /// invalid.
  87. /// - `FIRAuthErrorCodeMissingPhoneNumber` - Indicates that a phone number was not provided.
  88. @discardableResult
  89. func verifyPhoneNumber(_ phoneNumber: String,
  90. uiDelegate: AuthUIDelegate? = nil,
  91. multiFactorSession: MultiFactorSession?)
  92. -> Future<String, Error> {
  93. Future<String, Error> { promise in
  94. self.verifyPhoneNumber(
  95. phoneNumber,
  96. uiDelegate: uiDelegate,
  97. multiFactorSession: multiFactorSession
  98. ) { verificationID, error in
  99. if let error = error {
  100. promise(.failure(error))
  101. } else if let verificationID = verificationID {
  102. promise(.success(verificationID))
  103. }
  104. }
  105. }
  106. }
  107. /// Verify ownership of the second factor phone number by the current user.
  108. ///
  109. /// The publisher will emit events on the **main** thread.
  110. ///
  111. /// - Parameters:
  112. /// - phoneNumber: The phone number to be verified.
  113. /// - UIDelegate: An object used to present the SFSafariViewController. The object is retained
  114. /// by this method until the completion block is executed.
  115. /// - multiFactorSession: session A session to identify the MFA flow. For enrollment, this identifies the user
  116. /// trying to enroll. For sign-in, this identifies that the user already 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 = error {
  129. promise(.failure(error))
  130. } else if let verificationID = verificationID {
  131. promise(.success(verificationID))
  132. }
  133. }
  134. }
  135. }
  136. }
  137. #endif // os(iOS)
  138. #endif // canImport(Combine) && swift(>=5.0)