Skip to content

Commit 46cfaaa

Browse files
fix: make currentError a binding
1 parent f796218 commit 46cfaaa

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/AuthServiceError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public struct AccountConflictContext: LocalizedError, Identifiable, Equatable {
4242
public let underlyingError: Error
4343
public let message: String
4444
public let email: String?
45-
45+
4646
/// Human-readable description of the conflict type
4747
public var conflictDescription: String {
4848
switch conflictType {

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,20 @@ public final class AuthService {
130130
public var currentUser: User?
131131
public var authenticationState: AuthenticationState = .unauthenticated
132132
public var authenticationFlow: AuthenticationFlow = .signIn
133-
public private(set) var currentError: AlertError?
133+
private var _currentError: AlertError?
134+
135+
/// A binding that allows SwiftUI views to observe and clear errors
136+
public var currentError: Binding<AlertError?> {
137+
Binding(
138+
get: { self._currentError },
139+
set: { newValue in
140+
if newValue == nil {
141+
self._currentError = nil
142+
}
143+
}
144+
)
145+
}
146+
134147
public let passwordPrompt: PasswordPromptCoordinator = .init()
135148
public var currentMFARequired: MFARequired?
136149
private var currentMFAResolver: MultiFactorResolver?
@@ -207,12 +220,12 @@ public final class AuthService {
207220
}
208221

209222
func reset() {
210-
currentError = nil
223+
_currentError = nil
211224
currentAccountConflict = nil
212225
}
213226

214227
func updateError(title: String = "Error", message: String, underlyingError: Error? = nil) {
215-
currentError = AlertError(title: title, message: message, underlyingError: underlyingError)
228+
_currentError = AlertError(title: title, message: message, underlyingError: underlyingError)
216229
}
217230

218231
public var shouldHandleAnonymousUpgrade: Bool {
@@ -286,7 +299,7 @@ public final class AuthService {
286299
return handleMFARequiredError(resolver: resolver)
287300
}
288301
}
289-
302+
290303
// Possible conflicts from auth.signIn(with:):
291304
// - accountExistsWithDifferentCredential: account exists with different provider
292305
// - credentialAlreadyInUse: credential is already linked to another account
@@ -443,7 +456,7 @@ public extension AuthService {
443456
.invalidEmailLink("email address is missing from app storage. Is this the same device?")
444457
}
445458
let credential = EmailAuthProvider.credential(withEmail: email, link: link)
446-
459+
447460
// Possible conflicts from auth.signIn(withEmail:link:):
448461
// - accountExistsWithDifferentCredential: account exists with different provider
449462
// - credentialAlreadyInUse: credential is already linked to another account
@@ -883,8 +896,10 @@ public extension AuthService {
883896
/// - Parameters:
884897
/// - error: The error to check and handle
885898
/// - credential: The credential that caused the conflict
886-
/// - Throws: AuthServiceError.accountConflict if it's a conflict error, otherwise rethrows the original error
887-
private func handleErrorWithConflictCheck(error: Error, credential: AuthCredential) throws -> Never {
899+
/// - Throws: AuthServiceError.accountConflict if it's a conflict error, otherwise rethrows the
900+
/// original error
901+
private func handleErrorWithConflictCheck(error: Error,
902+
credential: AuthCredential) throws -> Never {
888903
// Check for account conflict errors
889904
if let error = error as NSError?,
890905
let conflictType = determineConflictType(from: error) {
@@ -893,15 +908,15 @@ public extension AuthService {
893908
conflictType: conflictType,
894909
credential: credential
895910
)
896-
911+
897912
// Store it for consumers to observe
898913
currentAccountConflict = context
899-
914+
900915
// Only set error alert if we're NOT auto-handling it
901916
if conflictType != .anonymousUpgradeConflict {
902917
updateError(message: context.message, underlyingError: error)
903918
}
904-
919+
905920
// Throw the specific error with context
906921
throw AuthServiceError.accountConflict(context)
907922
} else {

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ extension AuthPickerView: View {
145145
}
146146
}
147147
.errorAlert(
148-
error: $authService.currentError,
148+
error: authService.currentError,
149149
okButtonLabel: authService.string.okButtonLabel
150150
)
151151
}

0 commit comments

Comments
 (0)