Skip to content

Commit 48d8076

Browse files
chore: handle opinionated logic inside Views
1 parent a0a5e77 commit 48d8076

11 files changed

Lines changed: 81 additions & 351 deletions

File tree

FirebaseSwiftUI/FirebaseAppleSwiftUI/Sources/Views/SignInWithAppleButton.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import SwiftUI
2020
@MainActor
2121
public struct SignInWithAppleButton {
2222
@Environment(AuthService.self) private var authService
23-
@Environment(\.signInWithMergeConflictHandler) private var signInHandler
2423
let provider: AuthProviderSwift
2524
public init(provider: AuthProviderSwift) {
2625
self.provider = provider
@@ -35,13 +34,7 @@ extension SignInWithAppleButton: View {
3534
accessibilityId: "sign-in-with-apple-button"
3635
) {
3736
Task {
38-
if let handler = signInHandler {
39-
try? await handler(authService) {
40-
try await authService.signIn(provider)
41-
}
42-
} else {
43-
try? await authService.signIn(provider)
44-
}
37+
try? await authService.signIn(provider)
4538
}
4639
}
4740
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -245,24 +245,6 @@ public final class AuthService {
245245
}
246246
}
247247

248-
// Real account? → Requires proof of ownership → requiresManualLinking = true
249-
private func handleAccountExistsError(_ error: NSError, with credentials: AuthCredential) throws {
250-
let email = error.userInfo["FIRAuthErrorUserInfoEmailKey"] as? String
251-
let updatedCredential = error.userInfo["FIRAuthUpdatedCredentialKey"] as? AuthCredential
252-
?? credentials
253-
254-
let context = AccountMergeConflictContext(
255-
credential: updatedCredential,
256-
underlyingError: error,
257-
message: "An account already exists with \(email ?? "this email"). Please sign in with your existing method to link accounts.",
258-
uid: nil,
259-
email: email,
260-
requiresManualLinking: true
261-
)
262-
263-
throw AuthServiceError.accountMergeConflict(context: context)
264-
}
265-
266248
private func handleAutoUpgradeAnonymousUser(credentials: AuthCredential) async throws
267249
-> SignInOutcome {
268250
if currentUser == nil {
@@ -272,42 +254,8 @@ public final class AuthService {
272254
let result = try await currentUser?.link(with: credentials)
273255
updateAuthenticationState()
274256
return .signedIn(result)
275-
} catch let error as NSError {
276-
// Handle accountExistsWithDifferentCredential error
277-
if error.code == AuthErrorCode.accountExistsWithDifferentCredential.rawValue {
278-
try handleAccountExistsError(error, with: credentials)
279-
}
280-
281-
// Handle credentialAlreadyInUse error
282-
if error.code == AuthErrorCode.credentialAlreadyInUse.rawValue {
283-
// Extract the updated credential from the error
284-
let updatedCredential = error.userInfo["FIRAuthUpdatedCredentialKey"] as? AuthCredential
285-
?? credentials
286-
let context = AccountMergeConflictContext(
287-
credential: updatedCredential,
288-
underlyingError: error,
289-
message: "Unable to merge accounts. The credential is already associated with a different account.",
290-
uid: currentUser?.uid,
291-
email: nil,
292-
// Anonymous account? → Safe to discard → requiresManualLinking = false
293-
requiresManualLinking: false
294-
)
295-
throw AuthServiceError.accountMergeConflict(context: context)
296-
}
297-
298-
// Handle emailAlreadyInUse error
299-
if error.code == AuthErrorCode.emailAlreadyInUse.rawValue {
300-
let context = AccountMergeConflictContext(
301-
credential: credentials,
302-
underlyingError: error,
303-
message: "Unable to merge accounts. This email is already associated with a different account.",
304-
uid: currentUser?.uid,
305-
email: nil,
306-
// Anonymous account? → Safe to discard → requiresManualLinking = false
307-
requiresManualLinking: false
308-
)
309-
throw AuthServiceError.accountMergeConflict(context: context)
310-
}
257+
} catch {
258+
// Always throw errors - let view layer decide what to do
311259
throw error
312260
}
313261
}
@@ -325,11 +273,6 @@ public final class AuthService {
325273
} catch let error as NSError {
326274
authenticationState = .unauthenticated
327275

328-
// Handle account exists with different credential
329-
if error.code == AuthErrorCode.accountExistsWithDifferentCredential.rawValue {
330-
try handleAccountExistsError(error, with: credentials)
331-
}
332-
333276
// Check if this is an MFA required error
334277
if error.code == AuthErrorCode.secondFactorRequired.rawValue {
335278
if let resolver = error
@@ -427,7 +370,9 @@ public extension AuthService {
427370
}
428371
} catch {
429372
authenticationState = .unauthenticated
373+
// ALWAYS store error - let view layer decide what to do
430374
updateError(message: string.localizedErrorMessage(for: error), underlyingError: error)
375+
// ALWAYS throw - let view layer decide how to handle
431376
throw error
432377
}
433378
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AccountLinkingView.swift

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)