MultiFactor+Combine.swift 4.2 KB

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