Skip to content

[Auth]: Add auth state stream utility #14628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions FirebaseAuth/Sources/Swift/Auth/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,23 @@ extension Auth: AuthInterop {
}
}

public struct AuthState {
let auth: Auth
let user: FirebaseAuth.User?
}

open var authStateChanges: AsyncStream<AuthState> {
return AsyncStream { continuation in
let listener = self.addStateDidChangeListener { auth, user in
continuation.yield(AuthState(auth: auth, user: user))
}

continuation.onTermination = { _ in
self.removeStateDidChangeListener(listener)
}
}
}
Comment on lines +1369 to +1379
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider renaming this property to authStateChangesStream to clearly indicate that it is a stream of AuthState objects. This would improve readability and make the intent more explicit.

Suggested change
open var authStateChanges: AsyncStream<AuthState> {
return AsyncStream { continuation in
let listener = self.addStateDidChangeListener { auth, user in
continuation.yield(AuthState(auth: auth, user: user))
}
continuation.onTermination = { _ in
self.removeStateDidChangeListener(listener)
}
}
}
open var authStateChangesStream: AsyncStream<AuthState> {
return AsyncStream { continuation in
let listener = self.addStateDidChangeListener { auth, user in
continuation.yield(AuthState(auth: auth, user: user))
}
continuation.onTermination = { _ in
self.removeStateDidChangeListener(listener)
}
}
}


/// Unregisters a block as an "auth state did change" listener.
/// - Parameter listenerHandle: The handle for the listener.
@objc(removeAuthStateDidChangeListener:)
Expand Down
Loading