Skip to content

Commit 8696ca3

Browse files
refactor: use AuthServiceError for all errors
1 parent 56368ec commit 8696ca3

File tree

5 files changed

+22
-40
lines changed

5 files changed

+22
-40
lines changed

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/AuthServiceError.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public enum AuthServiceError: LocalizedError {
3838
case accountMergeConflict(context: AccountMergeConflictContext)
3939
case providerNotFound(String)
4040
case multiFactorAuth(String)
41+
case rootViewControllerNotFound(String)
42+
case providerAuthenticationFailed(String)
43+
case signInCancelled(String)
4144

4245
public var errorDescription: String? {
4346
switch self {
@@ -59,6 +62,8 @@ public enum AuthServiceError: LocalizedError {
5962
// Use when failed to sign-in with provider (e.g. Google, Facebook, etc.)
6063
case let .providerAuthenticationFailed(description):
6164
return description
65+
case let .signInCancelled(description):
66+
return description
6267
case let .accountMergeConflict(context):
6368
return context.errorDescription
6469
case let .providerNotFound(description):

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AccountService+Email.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ public final class PasswordPromptCoordinator {
9696

9797
func cancel() {
9898
continuation?
99-
.resume(throwing: AuthServiceError.reauthenticationRequired("Password entry cancelled"))
99+
.resume(throwing: AuthServiceError
100+
.signInCancelled("Password entry cancelled for Email provider"))
100101
cleanup()
101102
}
102103

FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderAuthUI.swift

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ import FirebaseAuth
1919
import FirebaseAuthSwiftUI
2020
import SwiftUI
2121

22-
public enum FacebookProviderError: Error {
23-
case signInCancelled(String)
24-
case configurationInvalid(String)
25-
case limitedLoginNonce(String)
26-
case accessToken(String)
27-
case authenticationToken(String)
28-
}
29-
3022
public class FacebookProviderSwift: AuthProviderSwift, DeleteUserSwift {
3123
let scopes: [String]
3224
let providerId = "facebook.com"
@@ -60,8 +52,8 @@ public class FacebookProviderSwift: AuthProviderSwift, DeleteUserSwift {
6052
)
6153
}
6254
}() else {
63-
throw FacebookProviderError
64-
.configurationInvalid("Failed to create Facebook login configuration")
55+
throw AuthServiceError
56+
.providerAuthenticationFailed("Failed to create Facebook login configuration")
6557
}
6658

6759
let result = try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<
@@ -74,7 +66,8 @@ public class FacebookProviderSwift: AuthProviderSwift, DeleteUserSwift {
7466
switch result {
7567
case .cancelled:
7668
continuation
77-
.resume(throwing: FacebookProviderError.signInCancelled("User cancelled sign-in"))
69+
.resume(throwing: AuthServiceError
70+
.signInCancelled("User cancelled sign-in for Facebook"))
7871
case let .failed(error):
7972
continuation.resume(throwing: error)
8073
case .success:
@@ -97,8 +90,8 @@ public class FacebookProviderSwift: AuthProviderSwift, DeleteUserSwift {
9790

9891
return credential
9992
} else {
100-
throw FacebookProviderError
101-
.accessToken(
93+
throw AuthServiceError
94+
.providerAuthenticationFailed(
10295
"Access token has expired or not available. Please sign-in with Facebook before attempting to create a Facebook provider credential"
10396
)
10497
}
@@ -107,16 +100,18 @@ public class FacebookProviderSwift: AuthProviderSwift, DeleteUserSwift {
107100
private func limitedLogin() throws -> AuthCredential {
108101
if let idToken = AuthenticationToken.current {
109102
guard let nonce = rawNonce else {
110-
throw FacebookProviderError
111-
.limitedLoginNonce("`rawNonce` has not been generated for Facebook limited login")
103+
throw AuthServiceError
104+
.providerAuthenticationFailed(
105+
"`rawNonce` has not been generated for Facebook limited login"
106+
)
112107
}
113108
let credential = OAuthProvider.credential(withProviderID: providerId,
114109
idToken: idToken.tokenString,
115110
rawNonce: nonce)
116111
return credential
117112
} else {
118-
throw FacebookProviderError
119-
.authenticationToken(
113+
throw AuthServiceError
114+
.providerAuthenticationFailed(
120115
"Authentication is not available. Please sign-in with Facebook before attempting to create a Facebook provider credential"
121116
)
122117
}

FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Views/SignInWithFacebookButton.swift

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,7 @@ extension SignInWithFacebookButton: View {
6666
VStack {
6767
Button(action: {
6868
Task {
69-
do {
70-
try await authService.signIn(facebookProvider)
71-
} catch {
72-
switch error {
73-
case FacebookProviderError.signInCancelled:
74-
showCanceledAlert = true
75-
default:
76-
// Error already handled by AuthService
77-
break
78-
}
79-
}
69+
try? await authService.signIn(facebookProvider)
8070
}
8171
}) {
8272
HStack {
@@ -113,12 +103,6 @@ extension SignInWithFacebookButton: View {
113103
.toggleStyle(SwitchToggleStyle(tint: .green))
114104
}
115105
}
116-
.alert(isPresented: $showCanceledAlert) {
117-
Alert(
118-
title: Text(authService.string.facebookLoginCancelledLabel),
119-
dismissButton: .default(Text(authService.string.okButtonLabel))
120-
)
121-
}
122106
.alert(isPresented: $showUserTrackingAlert) {
123107
Alert(
124108
title: Text(authService.string.authorizeUserTrackingLabel),

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Views/PhoneAuthView.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@ extension PhoneAuthView: View {
5151
HStack {
5252
Spacer()
5353
Button(action: {
54-
completion(.failure(NSError(
55-
domain: "PhoneAuthError",
56-
code: -1,
57-
userInfo: [NSLocalizedDescriptionKey: "User cancelled"]
58-
)))
54+
completion(.failure(AuthServiceError
55+
.signInCancelled("User cancelled sign-in for Phone")))
5956
dismiss()
6057
}) {
6158
Image(systemName: "xmark.circle.fill")

0 commit comments

Comments
 (0)