Skip to content

Commit 512668c

Browse files
fix: make account handler optional, throw error if not available
1 parent bda7eea commit 512668c

9 files changed

Lines changed: 62 additions & 35 deletions

File tree

FirebaseSwiftUI/FirebaseAppleSwiftUI/Sources/Views/SignInWithAppleButton.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ extension SignInWithAppleButton: View {
3737
Task {
3838
do {
3939
_ = try await authService.signIn(provider)
40-
} catch let AuthServiceError.accountConflict(context) {
41-
accountConflictHandler(context)
4240
} catch {
43-
// Other errors handled by .errorAlert()
41+
if case let AuthServiceError.accountConflict(context) = error,
42+
let handler = accountConflictHandler {
43+
handler(context)
44+
} else {
45+
throw error
46+
}
4447
}
4548
}
4649
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AccountConflictModifier.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import SwiftUI
1717

1818
/// Environment key for accessing the account conflict handler
1919
public struct AccountConflictHandlerKey: @preconcurrency EnvironmentKey {
20-
@MainActor public static let defaultValue: (AccountConflictContext) -> Void = { _ in }
20+
@MainActor public static let defaultValue: ((AccountConflictContext) -> Void)? = nil
2121
}
2222

2323
public extension EnvironmentValues {
24-
var accountConflictHandler: (AccountConflictContext) -> Void {
24+
var accountConflictHandler: ((AccountConflictContext) -> Void)? {
2525
get { self[AccountConflictHandlerKey.self] }
2626
set { self[AccountConflictHandlerKey.self] = newValue }
2727
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailAuthView.swift

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,29 @@ public struct EmailAuthView {
5050
}
5151
}
5252

53-
private func signInWithEmailPassword() async {
53+
private func signInWithEmailPassword() async throws {
5454
do {
5555
_ = try await authService.signIn(email: email, password: password)
56-
} catch let AuthServiceError.accountConflict(context) {
57-
accountConflictHandler(context)
5856
} catch {
59-
// Other errors handled by .errorAlert()
57+
if case let AuthServiceError.accountConflict(context) = error,
58+
let handler = accountConflictHandler {
59+
handler(context)
60+
} else {
61+
throw error
62+
}
6063
}
6164
}
6265

63-
private func createUserWithEmailPassword() async {
66+
private func createUserWithEmailPassword() async throws {
6467
do {
6568
_ = try await authService.createUser(email: email, password: password)
66-
} catch let AuthServiceError.accountConflict(context) {
67-
accountConflictHandler(context)
6869
} catch {
69-
// Other errors handled by .errorAlert()
70+
if case let AuthServiceError.accountConflict(context) = error,
71+
let handler = accountConflictHandler {
72+
handler(context)
73+
} else {
74+
throw error
75+
}
7076
}
7177
}
7278
}
@@ -96,7 +102,7 @@ extension EmailAuthView: View {
96102
contentType: .password,
97103
sensitive: true,
98104
onSubmit: { _ in
99-
Task { await signInWithEmailPassword() }
105+
Task { try? await signInWithEmailPassword() }
100106
},
101107
leading: {
102108
Image(systemName: "lock")
@@ -123,7 +129,7 @@ extension EmailAuthView: View {
123129
contentType: .password,
124130
sensitive: true,
125131
onSubmit: { _ in
126-
Task { await createUserWithEmailPassword() }
132+
Task { try? await createUserWithEmailPassword() }
127133
},
128134
leading: {
129135
Image(systemName: "lock")
@@ -137,9 +143,9 @@ extension EmailAuthView: View {
137143
Button(action: {
138144
Task {
139145
if authService.authenticationFlow == .signIn {
140-
await signInWithEmailPassword()
146+
try? await signInWithEmailPassword()
141147
} else {
142-
await createUserWithEmailPassword()
148+
try? await createUserWithEmailPassword()
143149
}
144150
}
145151
}) {

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailLinkView.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ extension EmailLinkView: View {
8989
Task {
9090
do {
9191
try await authService.handleSignInLink(url: url)
92-
} catch let AuthServiceError.accountConflict(context) {
93-
accountConflictHandler(context)
9492
} catch {
95-
// Other errors handled by .errorAlert()
93+
if case let AuthServiceError.accountConflict(context) = error,
94+
let handler = accountConflictHandler {
95+
handler(context)
96+
} else {
97+
throw error
98+
}
9699
}
97100
}
98101
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EnterVerificationCodeView.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ struct EnterVerificationCodeView: View {
6363

6464
_ = try await authService.signIn(credentials: credential)
6565
authService.navigator.clear()
66-
} catch let AuthServiceError.accountConflict(context) {
67-
accountConflictHandler(context)
6866
} catch {
69-
// Other errors handled by .errorAlert()
67+
if case let AuthServiceError.accountConflict(context) = error,
68+
let handler = accountConflictHandler {
69+
handler(context)
70+
} else {
71+
throw error
72+
}
7073
}
7174
}
7275
}) {

FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Views/SignInWithFacebookButton.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ extension SignInWithFacebookButton: View {
4040
Task {
4141
do {
4242
_ = try await authService.signIn(facebookProvider)
43-
} catch let AuthServiceError.accountConflict(context) {
44-
accountConflictHandler(context)
4543
} catch {
46-
// Other errors handled by .errorAlert()
44+
if case let AuthServiceError.accountConflict(context) = error,
45+
let handler = accountConflictHandler {
46+
handler(context)
47+
} else {
48+
throw error
49+
}
4750
}
4851
}
4952
}

FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Views/SignInWithGoogleButton.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ extension SignInWithGoogleButton: View {
4444
Task {
4545
do {
4646
_ = try await authService.signIn(googleProvider)
47-
} catch let AuthServiceError.accountConflict(context) {
48-
accountConflictHandler(context)
4947
} catch {
50-
// Other errors handled by .errorAlert()
48+
if case let AuthServiceError.accountConflict(context) = error,
49+
let handler = accountConflictHandler {
50+
handler(context)
51+
} else {
52+
throw error
53+
}
5154
}
5255
}
5356
}

FirebaseSwiftUI/FirebaseOAuthSwiftUI/Sources/Views/GenericOAuthButton.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ extension GenericOAuthButton: View {
5454
Task {
5555
do {
5656
_ = try await authService.signIn(provider)
57-
} catch let AuthServiceError.accountConflict(context) {
58-
accountConflictHandler(context)
5957
} catch {
60-
// Other errors handled by .errorAlert()
58+
if case let AuthServiceError.accountConflict(context) = error,
59+
let handler = accountConflictHandler {
60+
handler(context)
61+
} else {
62+
throw error
63+
}
6164
}
6265
}
6366
}

FirebaseSwiftUI/FirebaseTwitterSwiftUI/Sources/Views/SignInWithTwitterButton.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ extension SignInWithTwitterButton: View {
3737
Task {
3838
do {
3939
_ = try await authService.signIn(provider)
40-
} catch let AuthServiceError.accountConflict(context) {
41-
accountConflictHandler(context)
4240
} catch {
43-
// Other errors handled by .errorAlert()
41+
if case let AuthServiceError.accountConflict(context) = error,
42+
let handler = accountConflictHandler {
43+
handler(context)
44+
} else {
45+
throw error
46+
}
4447
}
4548
}
4649
}

0 commit comments

Comments
 (0)