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