MultiFactor+Combine.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2021 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) || targetEnvironment(macCatalyst)
  16. import Combine
  17. import FirebaseAuth
  18. @available(swift 5.0)
  19. @available(iOS 13.0, macCatalyst 13.0, *)
  20. @available(macOS, unavailable)
  21. @available(tvOS, unavailable)
  22. @available(watchOS, unavailable)
  23. public extension MultiFactor {
  24. /// Get a session for a second factor enrollment operation.
  25. ///
  26. /// The publisher will emit events on the **main** thread.
  27. ///
  28. /// - Returns: A publisher that emits a `MultiFactorSession` for a second factor
  29. /// enrollment operation. This is used to identify the current user trying to enroll a
  30. /// second factor. The publisher will emit on
  31. /// the *main* thread.
  32. @discardableResult
  33. func getSession() -> Future<MultiFactorSession, Error> {
  34. Future<MultiFactorSession, Error> { promise in
  35. self.getSessionWithCompletion { session, error in
  36. if let session = session {
  37. promise(.success(session))
  38. } else if let error = error {
  39. promise(.failure(error))
  40. }
  41. }
  42. }
  43. }
  44. /// Enrolls a second factor as identified by the `MultiFactorAssertion` parameter for the
  45. /// current user.
  46. ///
  47. /// The publisher will emit events on the **main** thread.
  48. ///
  49. /// - Parameters:
  50. /// - assertion: The base class for asserting ownership of a second factor.
  51. /// - displayName: An optional display name associated with the multi factor to enroll.
  52. ///
  53. /// - Returns: A publisher that emits whether the call was successful or not. The publisher
  54. /// will emit on the *main* thread.
  55. @discardableResult
  56. func enroll(with assertion: MultiFactorAssertion,
  57. displayName: String?) -> Future<Void, Error> {
  58. Future<Void, Error> { promise in
  59. self.enroll(with: assertion, displayName: displayName) { error in
  60. if let error = error {
  61. promise(.failure(error))
  62. } else {
  63. promise(.success(()))
  64. }
  65. }
  66. }
  67. }
  68. /// Unenroll the given multi factor.
  69. ///
  70. /// The publisher will emit events on the **main** thread.
  71. ///
  72. /// - Parameter factorInfo: The structure used to represent a second factor entity from a
  73. /// client perspective.
  74. /// - Returns: A publisher that emits when the request to send the unenrollment verification
  75. /// email is complete. The publisher
  76. /// will emit on the *main* thread.
  77. @discardableResult
  78. func unenroll(with factorInfo: MultiFactorInfo) -> Future<Void, Error> {
  79. Future<Void, Error> { promise in
  80. self.unenroll(with: factorInfo) { error in
  81. if let error = error {
  82. promise(.failure(error))
  83. } else {
  84. promise(.success(()))
  85. }
  86. }
  87. }
  88. }
  89. /// Unenroll the given multi factor.
  90. ///
  91. /// The publisher will emit events on the **main** thread.
  92. ///
  93. /// - Returns: A publisher that emits when the request to send the unenrollment verification
  94. /// email is complete.
  95. /// The publisher will emit on the *main* thread.
  96. @discardableResult
  97. func unenroll(withFactorUID factorUID: String) -> Future<Void, Error> {
  98. Future<Void, Error> { promise in
  99. self.unenroll(withFactorUID: factorUID) { error in
  100. if let error = error {
  101. promise(.failure(error))
  102. } else {
  103. promise(.success(()))
  104. }
  105. }
  106. }
  107. }
  108. }
  109. #endif // os(iOS) || targetEnvironment(macCatalyst)
  110. #endif // canImport(Combine) && swift(>=5.0)