Skip to content

Commit a3ffc12

Browse files
refactor: google provider
1 parent d1602f8 commit a3ffc12

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
lines changed

FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AccountService+Google.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import FirebaseAuthSwiftUI
3131
import Observation
3232

3333
protocol GoogleOperationReauthentication {
34-
var googleProvider: GoogleProviderAuthUI { get }
34+
var googleProvider: GoogleProviderSwift { get }
3535
}
3636

3737
extension GoogleOperationReauthentication {
@@ -54,8 +54,8 @@ extension GoogleOperationReauthentication {
5454
@MainActor
5555
class GoogleDeleteUserOperation: AuthenticatedOperation,
5656
@preconcurrency GoogleOperationReauthentication {
57-
let googleProvider: GoogleProviderAuthUI
58-
init(googleProvider: GoogleProviderAuthUI) {
57+
let googleProvider: GoogleProviderSwift
58+
init(googleProvider: GoogleProviderSwift) {
5959
self.googleProvider = googleProvider
6060
}
6161

FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AuthService+Google.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import FirebaseAuthSwiftUI
2323

2424
public extension AuthService {
2525
@discardableResult
26-
func withGoogleSignIn(scopes scopes: [String]? = nil) -> AuthService {
27-
let clientID = auth.app?.options.clientID ?? ""
28-
registerProvider(providerWithButton: GoogleProviderAuthUI(scopes: scopes, clientID: clientID))
26+
func withGoogleSignIn(_ provider: GoogleProviderSwift? = nil) -> AuthService {
27+
registerProvider(providerWithButton: GoogleProviderAuthUI(provider: provider ??
28+
GoogleProviderSwift(clientID: auth.app?.options.clientID ?? "")))
2929
return self
3030
}
3131
}

FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderAuthUI.swift

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,24 @@ import GoogleSignIn
1919
import GoogleSignInSwift
2020
import SwiftUI
2121

22-
let kGoogleUserInfoEmailScope = "https://www.googleapis.com/auth/userinfo.email"
23-
let kGoogleUserInfoProfileScope = "https://www.googleapis.com/auth/userinfo.profile"
24-
let kDefaultScopes = [kGoogleUserInfoEmailScope, kGoogleUserInfoProfileScope]
25-
2622
public enum GoogleProviderError: Error {
2723
case rootViewControllerNotFound(String)
2824
case authenticationToken(String)
2925
case user(String)
3026
}
3127

32-
public class GoogleProviderAuthUI: AuthProviderSwift, AuthProviderUI, DeleteUserSwift {
33-
public let id: String = "google"
28+
public class GoogleProviderSwift: AuthProviderSwift, DeleteUserSwift {
3429
let scopes: [String]
35-
let shortName = "Google"
30+
let clientID: String
3631
let providerId = "google.com"
37-
public let clientID: String
38-
39-
public var provider: AuthProviderSwift { self }
40-
41-
public init(scopes: [String]? = nil, clientID: String = FirebaseApp.app()!.options.clientID!) {
42-
self.scopes = scopes ?? kDefaultScopes
43-
self.clientID = clientID
44-
}
45-
46-
@MainActor public func authButton() -> AnyView {
47-
AnyView(SignInWithGoogleButton())
48-
}
4932

50-
public func deleteUser(user: User) async throws {
51-
let operation = GoogleDeleteUserOperation(googleProvider: self)
52-
try await operation(on: user)
33+
public init(scopes: [String] = [
34+
"https://www.googleapis.com/auth/userinfo.email",
35+
"https://www.googleapis.com/auth/userinfo.profile",
36+
],
37+
clientID: String) {
38+
self.clientID = clientID
39+
self.scopes = scopes
5340
}
5441

5542
@MainActor public func createAuthCredential() async throws -> AuthCredential {
@@ -61,7 +48,7 @@ public class GoogleProviderAuthUI: AuthProviderSwift, AuthProviderUI, DeleteUser
6148
)
6249
}
6350

64-
let config = GIDConfiguration(clientID: self.clientID)
51+
let config = GIDConfiguration(clientID: clientID)
6552
GIDSignIn.sharedInstance.configuration = config
6653

6754
return try await withCheckedThrowingContinuation { continuation in
@@ -86,4 +73,22 @@ public class GoogleProviderAuthUI: AuthProviderSwift, AuthProviderUI, DeleteUser
8673
}
8774
}
8875
}
76+
77+
public func deleteUser(user: User) async throws {
78+
let operation = GoogleDeleteUserOperation(googleProvider: self)
79+
try await operation(on: user)
80+
}
81+
}
82+
83+
public class GoogleProviderAuthUI: AuthProviderUI {
84+
public var provider: AuthProviderSwift
85+
public let id: String = "google.com"
86+
87+
public init(provider: AuthProviderSwift) {
88+
self.provider = provider
89+
}
90+
91+
@MainActor public func authButton() -> AnyView {
92+
AnyView(SignInWithGoogleButton(googleProvider: provider))
93+
}
8994
}

FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Views/SignInWithGoogleButton.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import SwiftUI
2626
@MainActor
2727
public struct SignInWithGoogleButton {
2828
@Environment(AuthService.self) private var authService
29-
private let googleProvider = GoogleProviderAuthUI()
29+
let googleProvider: AuthProviderSwift
30+
31+
public init(googleProvider: AuthProviderSwift) {
32+
self.googleProvider = googleProvider
33+
}
3034

3135
let customViewModel = GoogleSignInButtonViewModel(
3236
scheme: .light,
@@ -48,6 +52,7 @@ extension SignInWithGoogleButton: View {
4852

4953
#Preview {
5054
FirebaseOptions.dummyConfigurationForPreview()
51-
return SignInWithGoogleButton()
55+
let googleProvider = GoogleProviderSwift(clientID: "")
56+
return SignInWithGoogleButton(googleProvider: googleProvider)
5257
.environment(AuthService())
5358
}

0 commit comments

Comments
 (0)