Observe Firebase auth state (async/await)
Swift, Firebase, Async/await
swift
import Firebase
public extension User {
func delete() async throws {
// swiftformat:disable:next redundantReturn
return try await withCheckedThrowingContinuation { continuation in
delete { error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume()
}
}
}
}
}
How to use?
for await user in Auth.auth().observeStateDidChange() {
if let user {
print("User with id: \(user.uid) logged in")
} else {
print("User logged out")
}
}