Auth+Async.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2025 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. import Foundation
  15. public extension Auth {
  16. /// An asynchronous sequence of authentication state changes.
  17. ///
  18. /// This sequence provides a modern, `async/await`-compatible way to monitor the authentication
  19. /// state of the current user. It emits a new `User?` value whenever the user signs in or
  20. /// out.
  21. ///
  22. /// The sequence's underlying listener is automatically managed. It is added to the `Auth`
  23. /// instance when you begin iterating over the sequence and is removed when the iteration
  24. /// is cancelled or terminates.
  25. ///
  26. /// - Important: The first value emitted by this sequence is always the *current* authentication
  27. /// state, which may be `nil` if no user is signed in.
  28. ///
  29. /// ### Example Usage
  30. ///
  31. /// You can use a `for await` loop to handle authentication changes:
  32. ///
  33. /// ```swift
  34. /// func monitorAuthState() async {
  35. /// for await user in Auth.auth().authStateChanges {
  36. /// if let user = user {
  37. /// print("User signed in: \(user.uid)")
  38. /// // Update UI or perform actions for a signed-in user.
  39. /// } else {
  40. /// print("User signed out.")
  41. /// // Update UI or perform actions for a signed-out state.
  42. /// }
  43. /// }
  44. /// }
  45. /// ```
  46. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  47. var authStateChanges: some AsyncSequence<User?, Never> {
  48. AsyncStream { continuation in
  49. let listenerHandle = addStateDidChangeListener { _, user in
  50. continuation.yield(user)
  51. }
  52. continuation.onTermination = { @Sendable _ in
  53. self.removeStateDidChangeListener(listenerHandle)
  54. }
  55. }
  56. }
  57. /// An asynchronous sequence of ID token changes.
  58. ///
  59. /// This sequence provides a modern, `async/await`-compatible way to monitor changes to the
  60. /// current user's ID token. It emits a new `User?` value whenever the ID token changes.
  61. ///
  62. /// The sequence's underlying listener is automatically managed. It is added to the `Auth`
  63. /// instance when you begin iterating over the sequence and is removed when the iteration
  64. /// is cancelled or terminates.
  65. ///
  66. /// - Important: The first value emitted by this sequence is always the *current* authentication
  67. /// state, which may be `nil` if no user is signed in.
  68. ///
  69. /// ### Example Usage
  70. ///
  71. /// You can use a `for await` loop to handle ID token changes:
  72. ///
  73. /// ```swift
  74. /// func monitorIDTokenChanges() async {
  75. /// for await user in Auth.auth().idTokenChanges {
  76. /// if let user = user {
  77. /// print("ID token changed for user: \(user.uid)")
  78. /// // Update UI or perform actions for a signed-in user.
  79. /// } else {
  80. /// print("User signed out.")
  81. /// // Update UI or perform actions for a signed-out state.
  82. /// }
  83. /// }
  84. /// }
  85. /// ```
  86. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  87. var idTokenChanges: some AsyncSequence<User?, Never> {
  88. AsyncStream { continuation in
  89. let listenerHandle = addIDTokenDidChangeListener { _, user in
  90. continuation.yield(user)
  91. }
  92. continuation.onTermination = { @Sendable _ in
  93. self.removeIDTokenDidChangeListener(listenerHandle)
  94. }
  95. }
  96. }
  97. }