Skip to content

Commit f1fcaeb

Browse files
initial setup of google auth sign-in
1 parent fb7d515 commit f1fcaeb

5 files changed

Lines changed: 68 additions & 6 deletions

File tree

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import SwiftUI
33

44
public protocol GoogleProviderProtocol {
55
func handleUrl(_ url: URL) -> Bool
6+
func signInWithGoogle(clientID: String)
67
}
78

89
public enum AuthenticationProvider {
@@ -100,6 +101,23 @@ public final class AuthService {
100101
updateAuthenticationState()
101102
}
102103

104+
public func signInWithGoogle() async throws {
105+
authenticationState = .authenticating
106+
do {
107+
guard let clientID = auth.app?.options.clientID else { throw NSError(
108+
domain: "AuthServiceErrorDomain",
109+
code: 2,
110+
userInfo: [
111+
NSLocalizedDescriptionKey: "OAuth client ID not found. Please make sure Google Sign-In is enabled in the Firebase console. You may have to download a new GoogleService-Info.plist file after enabling Google Sign-In.",
112+
]
113+
) }
114+
try safeGoogleProvider.signInWithGoogle(clientID: clientID)
115+
} catch {
116+
authenticationState = .unauthenticated
117+
throw error
118+
}
119+
}
120+
103121
func signIn(with credentials: AuthCredential) async throws {
104122
authenticationState = .authenticating
105123
do {

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Utils/StringUtils.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ let kAccountDisabledError = "AccountDisabledError"
1313
let kEmailsDoNotMatchError = "EmailsDoNotMatchError"
1414
let kUnknownError = "UnknownError"
1515

16-
class StringUtils {
17-
static func localizedString(forKey key: String, configuration: AuthConfiguration) -> String {
16+
public class StringUtils {
17+
public static func localizedString(forKey key: String,
18+
configuration: AuthConfiguration) -> String {
1819
if let customStringsBundle = configuration.customStringsBundle {
1920
let localizedString = customStringsBundle.localizedString(
2021
forKey: key,
@@ -30,7 +31,8 @@ class StringUtils {
3031
return Bundle.module.localizedString(forKey: key, value: nil, table: nil)
3132
}
3233

33-
static func localizedErrorMessage(for error: Error, configuration: AuthConfiguration) -> String {
34+
public static func localizedErrorMessage(for error: Error,
35+
configuration: AuthConfiguration) -> String {
3436
let authError = error as NSError
3537
let errorCode = AuthErrorCode(rawValue: authError.code)
3638
switch errorCode {

FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/FirebaseGoogleSwiftUI.swift renamed to FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let kGoogleUserInfoEmailScope = "https://www.googleapis.com/auth/userinfo.email"
66
let kGoogleUserInfoProfileScope = "https://www.googleapis.com/auth/userinfo.profile"
77
let kDefaultScopes = [kGoogleUserInfoEmailScope, kGoogleUserInfoProfileScope]
88

9-
public class GoogleProviderSwift: GoogleProviderProtocol {
9+
public class GoogleProviderSwift: @preconcurrency GoogleProviderProtocol {
1010
let scopes: [String]
1111
let shortName = "Google"
1212
let providerId = "google.com"
@@ -45,7 +45,6 @@ public class GoogleProviderSwift: GoogleProviderProtocol {
4545
let credential = GoogleAuthProvider.credential(withIDToken: idToken,
4646
accessToken: user.accessToken
4747
.tokenString)
48-
4948
}
5049
}
5150
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import FirebaseAuthSwiftUI
2+
import SwiftUI
3+
4+
@MainActor
5+
public struct GoogleButtonView {
6+
@Environment(AuthService.self) private var authService
7+
@State private var errorMessage = ""
8+
9+
public init() {}
10+
11+
private func signInWithGoogle() async {
12+
do {
13+
try await authService.signInWithGoogle()
14+
} catch {
15+
errorMessage = StringUtils.localizedErrorMessage(
16+
for: error,
17+
configuration: authService.configuration
18+
)
19+
}
20+
}
21+
}
22+
23+
extension GoogleButtonView: View {
24+
public var body: some View {
25+
Button(action: {
26+
Task {
27+
try await signInWithGoogle()
28+
}
29+
}) {
30+
if authService.authenticationState != .authenticating {
31+
Text(authService.authenticationFlow == .login ? "Login with Google" : "Sign-up with Google")
32+
.padding(.vertical, 8)
33+
.frame(maxWidth: .infinity)
34+
} else {
35+
ProgressView()
36+
.progressViewStyle(CircularProgressViewStyle(tint: .white))
37+
.padding(.vertical, 8)
38+
.frame(maxWidth: .infinity)
39+
}
40+
}
41+
Text(errorMessage)
42+
}
43+
}

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/FirebaseSwiftUIExampleApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct ContentView: View {
4848

4949
var body: some View {
5050
AuthPickerView {
51-
Text("GOOGLE AUTH BUTTON")
51+
GoogleButtonView()
5252
}.environment(authService)
5353
}
5454
}

0 commit comments

Comments
 (0)