Skip to content

Commit cda7c4c

Browse files
Merge pull request #1281 from firebase/unified-auth-service-error
2 parents 66bc390 + 8696ca3 commit cda7c4c

File tree

8 files changed

+34
-52
lines changed

8 files changed

+34
-52
lines changed

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/AuthServiceError.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ public enum AuthServiceError: LocalizedError {
3636
case invalidCredentials(String)
3737
case signInFailed(underlying: Error)
3838
case accountMergeConflict(context: AccountMergeConflictContext)
39-
case invalidPhoneAuthenticationArguments(String)
4039
case providerNotFound(String)
4140
case multiFactorAuth(String)
41+
case rootViewControllerNotFound(String)
42+
case providerAuthenticationFailed(String)
43+
case signInCancelled(String)
4244

4345
public var errorDescription: String? {
4446
switch self {
@@ -54,16 +56,22 @@ public enum AuthServiceError: LocalizedError {
5456
return description
5557
case let .invalidCredentials(description):
5658
return description
59+
// Use when failed to sign-in with Firebase
5760
case let .signInFailed(underlying: error):
5861
return "Failed to sign in: \(error.localizedDescription)"
62+
// Use when failed to sign-in with provider (e.g. Google, Facebook, etc.)
63+
case let .providerAuthenticationFailed(description):
64+
return description
65+
case let .signInCancelled(description):
66+
return description
5967
case let .accountMergeConflict(context):
6068
return context.errorDescription
6169
case let .providerNotFound(description):
6270
return description
63-
case let .invalidPhoneAuthenticationArguments(description):
64-
return description
6571
case let .multiFactorAuth(description):
6672
return description
73+
case let .rootViewControllerNotFound(description):
74+
return description
6775
}
6876
}
6977
}

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/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderAuthUI.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ import GoogleSignIn
1919
import GoogleSignInSwift
2020
import SwiftUI
2121

22-
public enum GoogleProviderError: Error {
23-
case rootViewControllerNotFound(String)
24-
case authenticationToken(String)
25-
case user(String)
26-
}
27-
2822
public class GoogleProviderSwift: AuthProviderSwift, DeleteUserSwift {
2923
let scopes: [String]
3024
let clientID: String
@@ -42,7 +36,7 @@ public class GoogleProviderSwift: AuthProviderSwift, DeleteUserSwift {
4236
@MainActor public func createAuthCredential() async throws -> AuthCredential {
4337
guard let presentingViewController = await (UIApplication.shared.connectedScenes
4438
.first as? UIWindowScene)?.windows.first?.rootViewController else {
45-
throw GoogleProviderError
39+
throw AuthServiceError
4640
.rootViewControllerNotFound(
4741
"Root View controller is not available to present Google sign-in View."
4842
)
@@ -63,7 +57,8 @@ public class GoogleProviderSwift: AuthProviderSwift, DeleteUserSwift {
6357
guard let user = result?.user,
6458
let idToken = user.idToken?.tokenString else {
6559
continuation
66-
.resume(throwing: GoogleProviderError.user("Failed to retrieve user or idToken."))
60+
.resume(throwing: AuthServiceError
61+
.providerAuthenticationFailed("Failed to retrieve user or idToken."))
6762
return
6863
}
6964

FirebaseSwiftUI/FirebaseOAuthSwiftUI/Sources/Services/OAuthProviderSwift.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class OAuthProviderSwift: AuthProviderSwift, DeleteUserSwift {
5151
self.buttonBackgroundColor = buttonBackgroundColor
5252
self.buttonForegroundColor = buttonForegroundColor
5353
}
54+
5455
/// Convenience initializer using SF Symbol
5556
/// - Parameters:
5657
/// - providerId: The OAuth provider ID (e.g., "github.com", "microsoft.com")
@@ -132,6 +133,7 @@ public class OAuthProviderAuthUI: AuthProviderUI {
132133
}
133134
return oauthProvider.providerId
134135
}
136+
135137
@MainActor public func authButton() -> AnyView {
136138
AnyView(GenericOAuthButton(provider: provider))
137139
}

FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderAuthUI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class PhoneProviderSwift: PhoneAuthProviderSwift {
3939
guard let presentingViewController = await (UIApplication.shared.connectedScenes
4040
.first as? UIWindowScene)?.windows.first?.rootViewController else {
4141
throw AuthServiceError
42-
.invalidPhoneAuthenticationArguments(
42+
.rootViewControllerNotFound(
4343
"Root View controller is not available to present Phone auth View."
4444
)
4545
}

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)