From 5be4e0d10d2eab2420f8de7076a952d53808b304 Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Thu, 17 Apr 2025 08:28:20 -0700 Subject: [PATCH 1/3] move buttons out of viewbuilder --- .../Sources/Services/AuthService.swift | 17 ++++++--- .../Sources/Strings/Localizable.xcstrings | 1 + .../Services/FacebookProviderSwift.swift | 5 +++ .../Sources/Services/AuthService+Google.swift | 19 ++++++++++ .../Services/GoogleProviderSwift.swift | 14 ++++++- Package.swift | 1 + .../project.pbxproj | 38 +++++++++---------- .../FirebaseSwiftUIExampleApp.swift | 2 - 8 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AuthService+Google.swift diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift index ec2b94961b..87a9be4822 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift @@ -1,16 +1,20 @@ @preconcurrency import FirebaseAuth import SwiftUI -public protocol GoogleProviderProtocol { +public protocol ExternalAuthProvider { + @MainActor var authButton: any View { get } +} + +public protocol GoogleProviderProtocol: ExternalAuthProvider { func handleUrl(_ url: URL) -> Bool @MainActor func signInWithGoogle(clientID: String) async throws -> AuthCredential } -public protocol FacebookProviderProtocol { +public protocol FacebookProviderProtocol: ExternalAuthProvider { @MainActor func signInWithFacebook(isLimitedLogin: Bool) async throws -> AuthCredential } -public protocol PhoneAuthProviderProtocol { +public protocol PhoneAuthProviderProtocol: ExternalAuthProvider { @MainActor func verifyPhoneNumber(phoneNumber: String) async throws -> String } @@ -90,10 +94,11 @@ public final class AuthService { public var authenticationFlow: AuthenticationFlow = .login public var errorMessage = "" + public var googleProvider: GoogleProviderProtocol? + public var facebookProvider: FacebookProviderProtocol? + public var phoneAuthProvider: PhoneAuthProviderProtocol? + private var listenerManager: AuthListenerManager? - private let googleProvider: GoogleProviderProtocol? - private let facebookProvider: FacebookProviderProtocol? - private let phoneAuthProvider: PhoneAuthProviderProtocol? private var signedInCredential: AuthCredential? private var safeGoogleProvider: GoogleProviderProtocol { diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Strings/Localizable.xcstrings b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Strings/Localizable.xcstrings index a4b95dde64..c9a4b44831 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Strings/Localizable.xcstrings +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Strings/Localizable.xcstrings @@ -162,6 +162,7 @@ }, "AuthPickerTitle" : { "comment" : "Title for auth picker screen.", + "extractionState" : "stale", "localizations" : { "en" : { "stringUnit" : { diff --git a/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift b/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift index c72887efae..2c62f2ba09 100644 --- a/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift +++ b/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift @@ -3,6 +3,7 @@ import FacebookCore import FacebookLogin import FirebaseAuth import FirebaseAuthSwiftUI +import SwiftUI let kFacebookEmailScope = "email" let kFacebookProfileScope = "public_profile" @@ -34,6 +35,10 @@ public class FacebookProviderSwift: FacebookProviderProtocol { shaNonce = CommonUtils.sha256Hash(of: rawNonce) } + @MainActor public var authButton: any View { + return FacebookButtonView() + } + @MainActor public func signInWithFacebook(isLimitedLogin: Bool) async throws -> AuthCredential { let trackingStatus = ATTrackingManager.trackingAuthorizationStatus let tracking: LoginTracking = trackingStatus != .authorized ? .limited : diff --git a/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AuthService+Google.swift b/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AuthService+Google.swift new file mode 100644 index 0000000000..1690add168 --- /dev/null +++ b/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AuthService+Google.swift @@ -0,0 +1,19 @@ +// +// AuthService+Google.swift +// FirebaseUI +// +// Created by Morgan Chen on 4/16/25. +// + +import FirebaseAuthSwiftUI + +extension AuthService { + + @discardableResult + func withGoogleSignIn() -> AuthService { + let clientID = auth.app?.options.clientID ?? "" + self.googleProvider = GoogleProviderSwift(clientID: clientID) + return self + } + +} diff --git a/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift b/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift index f042e95e71..bc235f9774 100644 --- a/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift +++ b/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift @@ -1,6 +1,8 @@ @preconcurrency import FirebaseAuth import FirebaseAuthSwiftUI import GoogleSignIn +import GoogleSignInSwift +import SwiftUI let kGoogleUserInfoEmailScope = "https://www.googleapis.com/auth/userinfo.email" let kGoogleUserInfoProfileScope = "https://www.googleapis.com/auth/userinfo.profile" @@ -16,14 +18,24 @@ public class GoogleProviderSwift: @preconcurrency GoogleProviderProtocol { let scopes: [String] let shortName = "Google" let providerId = "google.com" - public init(scopes: [String]? = nil) { + let clientID: String + public init(scopes: [String]? = nil, clientID: String) { self.scopes = scopes ?? kDefaultScopes + self.clientID = clientID } public func handleUrl(_ url: URL) -> Bool { return GIDSignIn.sharedInstance.handle(url) } + @MainActor public var authButton: any View { + return GoogleSignInButton { + Task { + try await self.signInWithGoogle(clientID: self.clientID) + } + } + } + @MainActor public func signInWithGoogle(clientID: String) async throws -> AuthCredential { guard let presentingViewController = await (UIApplication.shared.connectedScenes .first as? UIWindowScene)?.windows.first?.rootViewController else { diff --git a/Package.swift b/Package.swift index a629a986fb..c6b33ca343 100644 --- a/Package.swift +++ b/Package.swift @@ -277,6 +277,7 @@ let package = Package( dependencies: [ "FirebaseAuthSwiftUI", "GoogleSignIn", + .product(name: "GoogleSignInSwift", package: "GoogleSignIn") ], path: "FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources" ), diff --git a/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample.xcodeproj/project.pbxproj b/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample.xcodeproj/project.pbxproj index 390e704b85..2fb44425f3 100644 --- a/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample.xcodeproj/project.pbxproj +++ b/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample.xcodeproj/project.pbxproj @@ -9,11 +9,11 @@ /* Begin PBXBuildFile section */ 4607CC9C2D9BFE29009EC3F5 /* FirebaseAuthSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 4607CC9B2D9BFE29009EC3F5 /* FirebaseAuthSwiftUI */; }; 4607CC9E2D9BFE29009EC3F5 /* FirebaseGoogleSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 4607CC9D2D9BFE29009EC3F5 /* FirebaseGoogleSwiftUI */; }; - 4670DEA72D9EA9E100E0D36A /* FirebaseFacebookSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 4670DEA62D9EA9E100E0D36A /* FirebaseFacebookSwiftUI */; }; - 46C4EAB32DA801B200FC878B /* FirebasePhoneAuthSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 46C4EAB22DA801B200FC878B /* FirebasePhoneAuthSwiftUI */; }; 46CB7B252D773F2100F1FD0A /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 46CB7B242D773F2100F1FD0A /* GoogleService-Info.plist */; }; 46F89C392D64B04E000F8BC0 /* FirebaseAuthSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 46F89C382D64B04E000F8BC0 /* FirebaseAuthSwiftUI */; }; 46F89C4D2D64BB9B000F8BC0 /* FirebaseAuthSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 46F89C4C2D64BB9B000F8BC0 /* FirebaseAuthSwiftUI */; }; + 8D808CB72DB0811900D2293F /* FirebaseFacebookSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 8D808CB62DB0811900D2293F /* FirebaseFacebookSwiftUI */; }; + 8D808CB92DB081F900D2293F /* FirebasePhoneAuthSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 8D808CB82DB081F900D2293F /* FirebasePhoneAuthSwiftUI */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -76,11 +76,11 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 4670DEA72D9EA9E100E0D36A /* FirebaseFacebookSwiftUI in Frameworks */, + 8D808CB72DB0811900D2293F /* FirebaseFacebookSwiftUI in Frameworks */, 46F89C392D64B04E000F8BC0 /* FirebaseAuthSwiftUI in Frameworks */, 46F89C4D2D64BB9B000F8BC0 /* FirebaseAuthSwiftUI in Frameworks */, 4607CC9E2D9BFE29009EC3F5 /* FirebaseGoogleSwiftUI in Frameworks */, - 46C4EAB32DA801B200FC878B /* FirebasePhoneAuthSwiftUI in Frameworks */, + 8D808CB92DB081F900D2293F /* FirebasePhoneAuthSwiftUI in Frameworks */, 4607CC9C2D9BFE29009EC3F5 /* FirebaseAuthSwiftUI in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -155,8 +155,8 @@ 46F89C4C2D64BB9B000F8BC0 /* FirebaseAuthSwiftUI */, 4607CC9B2D9BFE29009EC3F5 /* FirebaseAuthSwiftUI */, 4607CC9D2D9BFE29009EC3F5 /* FirebaseGoogleSwiftUI */, - 4670DEA62D9EA9E100E0D36A /* FirebaseFacebookSwiftUI */, - 46C4EAB22DA801B200FC878B /* FirebasePhoneAuthSwiftUI */, + 8D808CB62DB0811900D2293F /* FirebaseFacebookSwiftUI */, + 8D808CB82DB081F900D2293F /* FirebasePhoneAuthSwiftUI */, ); productName = FirebaseSwiftUIExample; productReference = 46F89C082D64A86C000F8BC0 /* FirebaseSwiftUIExample.app */; @@ -241,7 +241,7 @@ mainGroup = 46F89BFF2D64A86C000F8BC0; minimizedProjectReferenceProxies = 1; packageReferences = ( - 4607CC9A2D9BFE29009EC3F5 /* XCLocalSwiftPackageReference "../../../../firebaseUI-ios" */, + 8D808CB52DB07EBD00D2293F /* XCLocalSwiftPackageReference "../../../../FirebaseUI-iOS" */, ); preferredProjectObjectVersion = 77; productRefGroup = 46F89C092D64A86C000F8BC0 /* Products */; @@ -617,9 +617,9 @@ /* End XCConfigurationList section */ /* Begin XCLocalSwiftPackageReference section */ - 4607CC9A2D9BFE29009EC3F5 /* XCLocalSwiftPackageReference "../../../../firebaseUI-ios" */ = { + 8D808CB52DB07EBD00D2293F /* XCLocalSwiftPackageReference "../../../../FirebaseUI-iOS" */ = { isa = XCLocalSwiftPackageReference; - relativePath = "../../../../firebaseUI-ios"; + relativePath = "../../../../FirebaseUI-iOS"; }; /* End XCLocalSwiftPackageReference section */ @@ -632,16 +632,6 @@ isa = XCSwiftPackageProductDependency; productName = FirebaseGoogleSwiftUI; }; - 4670DEA62D9EA9E100E0D36A /* FirebaseFacebookSwiftUI */ = { - isa = XCSwiftPackageProductDependency; - package = 4607CC9A2D9BFE29009EC3F5 /* XCLocalSwiftPackageReference "../../../../firebaseUI-ios" */; - productName = FirebaseFacebookSwiftUI; - }; - 46C4EAB22DA801B200FC878B /* FirebasePhoneAuthSwiftUI */ = { - isa = XCSwiftPackageProductDependency; - package = 4607CC9A2D9BFE29009EC3F5 /* XCLocalSwiftPackageReference "../../../../firebaseUI-ios" */; - productName = FirebasePhoneAuthSwiftUI; - }; 46F89C382D64B04E000F8BC0 /* FirebaseAuthSwiftUI */ = { isa = XCSwiftPackageProductDependency; productName = FirebaseAuthSwiftUI; @@ -650,6 +640,16 @@ isa = XCSwiftPackageProductDependency; productName = FirebaseAuthSwiftUI; }; + 8D808CB62DB0811900D2293F /* FirebaseFacebookSwiftUI */ = { + isa = XCSwiftPackageProductDependency; + package = 8D808CB52DB07EBD00D2293F /* XCLocalSwiftPackageReference "../../../../FirebaseUI-iOS" */; + productName = FirebaseFacebookSwiftUI; + }; + 8D808CB82DB081F900D2293F /* FirebasePhoneAuthSwiftUI */ = { + isa = XCSwiftPackageProductDependency; + package = 8D808CB52DB07EBD00D2293F /* XCLocalSwiftPackageReference "../../../../FirebaseUI-iOS" */; + productName = FirebasePhoneAuthSwiftUI; + }; /* End XCSwiftPackageProductDependency section */ }; rootObject = 46F89C002D64A86C000F8BC0 /* Project object */; diff --git a/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/FirebaseSwiftUIExampleApp.swift b/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/FirebaseSwiftUIExampleApp.swift index d2b53c0684..b22cad5bfd 100644 --- a/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/FirebaseSwiftUIExampleApp.swift +++ b/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/FirebaseSwiftUIExampleApp.swift @@ -15,8 +15,6 @@ import FirebasePhoneAuthSwiftUI import SwiftData import SwiftUI -let googleProvider = GoogleProviderSwift() - class AppDelegate: NSObject, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [ From 9355939e04a6e1c7c0dca249349ea4642503299c Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Mon, 21 Apr 2025 13:06:07 -0700 Subject: [PATCH 2/3] fix build errors --- .../Sources/Services/AuthService.swift | 1 - .../Sources/Strings/Localizable.xcstrings | 6 ++++++ .../Sources/Services/FacebookProviderSwift.swift | 2 +- .../Sources/Services/AuthService+Google.swift | 4 ++-- .../Sources/Services/GoogleProviderSwift.swift | 7 ++----- .../Sources/Services/PhoneAuthProviderSwift.swift | 7 +++++++ .../FirebaseSwiftUIExampleApp.swift | 13 +++++++++---- 7 files changed, 27 insertions(+), 13 deletions(-) diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift index d6038c664f..850fcb6e74 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift @@ -6,7 +6,6 @@ public protocol ExternalAuthProvider { } public protocol GoogleProviderProtocol: ExternalAuthProvider { - func handleUrl(_ url: URL) -> Bool @MainActor func signInWithGoogle(clientID: String) async throws -> AuthCredential } diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Strings/Localizable.xcstrings b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Strings/Localizable.xcstrings index c9a4b44831..b7e9918b39 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Strings/Localizable.xcstrings +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Strings/Localizable.xcstrings @@ -405,6 +405,9 @@ } } } + }, + "Enter Password" : { + }, "EnterYourEmail" : { "comment" : "Title for email entry screen, email text field placeholder. Use short/abbreviated translation for 'email' which is less than 15 chars.", @@ -933,6 +936,9 @@ } } } + }, + "Submit" : { + }, "TermsOfService" : { "comment" : "Text linked to a web page with the Terms of Service content.", diff --git a/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift b/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift index 2c62f2ba09..b47b06c44c 100644 --- a/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift +++ b/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift @@ -36,7 +36,7 @@ public class FacebookProviderSwift: FacebookProviderProtocol { } @MainActor public var authButton: any View { - return FacebookButtonView() + return SignInWithFacebookButton() } @MainActor public func signInWithFacebook(isLimitedLogin: Bool) async throws -> AuthCredential { diff --git a/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AuthService+Google.swift b/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AuthService+Google.swift index 1690add168..eba43c5a31 100644 --- a/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AuthService+Google.swift +++ b/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AuthService+Google.swift @@ -7,10 +7,10 @@ import FirebaseAuthSwiftUI -extension AuthService { +public extension AuthService { @discardableResult - func withGoogleSignIn() -> AuthService { + public func withGoogleSignIn() -> AuthService { let clientID = auth.app?.options.clientID ?? "" self.googleProvider = GoogleProviderSwift(clientID: clientID) return self diff --git a/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift b/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift index bc235f9774..b9c342619c 100644 --- a/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift +++ b/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift @@ -1,5 +1,6 @@ @preconcurrency import FirebaseAuth import FirebaseAuthSwiftUI +import FirebaseCore import GoogleSignIn import GoogleSignInSwift import SwiftUI @@ -19,15 +20,11 @@ public class GoogleProviderSwift: @preconcurrency GoogleProviderProtocol { let shortName = "Google" let providerId = "google.com" let clientID: String - public init(scopes: [String]? = nil, clientID: String) { + public init(scopes: [String]? = nil, clientID: String = FirebaseApp.app()!.options.clientID!) { self.scopes = scopes ?? kDefaultScopes self.clientID = clientID } - public func handleUrl(_ url: URL) -> Bool { - return GIDSignIn.sharedInstance.handle(url) - } - @MainActor public var authButton: any View { return GoogleSignInButton { Task { diff --git a/FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderSwift.swift b/FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderSwift.swift index df78c63ca8..9774c0319a 100644 --- a/FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderSwift.swift +++ b/FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderSwift.swift @@ -1,9 +1,16 @@ @preconcurrency import FirebaseAuth import FirebaseAuthSwiftUI +import SwiftUI public typealias VerificationID = String public class PhoneAuthProviderSwift: @preconcurrency PhoneAuthProviderProtocol { + + public var authButton: any View { + // TODO: implement me + return Button("Phone", action: { }) + } + public init() {} @MainActor public func verifyPhoneNumber(phoneNumber: String) async throws -> VerificationID { diff --git a/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/FirebaseSwiftUIExampleApp.swift b/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/FirebaseSwiftUIExampleApp.swift index 0a084a555b..efc30d169a 100644 --- a/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/FirebaseSwiftUIExampleApp.swift +++ b/samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample/FirebaseSwiftUIExampleApp.swift @@ -12,6 +12,7 @@ import FirebaseCore import FirebaseFacebookSwiftUI import FirebaseGoogleSwiftUI import FirebasePhoneAuthSwiftUI +import GoogleSignIn import SwiftData import SwiftUI @@ -45,15 +46,17 @@ class AppDelegate: NSObject, UIApplicationDelegate { func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { - ApplicationDelegate.shared.application( + if ApplicationDelegate.shared.application( app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey .sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation] - ) + ) { + return true + } - return googleProvider.handleUrl(url) + return GIDSignIn.sharedInstance.handle(url) } } @@ -92,10 +95,12 @@ struct ContentView: View { let phoneAuthProvider = PhoneAuthProviderSwift() authService = AuthService( configuration: configuration, - googleProvider: googleProvider, + googleProvider: nil, facebookProvider: facebookProvider, phoneAuthProvider: phoneAuthProvider ) + // Transition to this api + .withGoogleSignIn() } var body: some View { From a6bafd9eb504432b8ce0284b774030462ccda073 Mon Sep 17 00:00:00 2001 From: Morgan Chen Date: Mon, 28 Apr 2025 11:31:25 -0700 Subject: [PATCH 3/3] avoid any view --- .../Sources/Services/AuthService.swift | 21 ++++++++++--------- .../Services/FacebookProviderSwift.swift | 2 +- .../Services/GoogleProviderSwift.swift | 2 +- .../Services/PhoneAuthProviderSwift.swift | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift index 850fcb6e74..d4148703a5 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift @@ -2,7 +2,8 @@ import SwiftUI public protocol ExternalAuthProvider { - @MainActor var authButton: any View { get } + associatedtype ButtonType: View + @MainActor var authButton: ButtonType { get } } public protocol GoogleProviderProtocol: ExternalAuthProvider { @@ -64,9 +65,9 @@ private final class AuthListenerManager { @Observable public final class AuthService { public init(configuration: AuthConfiguration = AuthConfiguration(), auth: Auth = Auth.auth(), - googleProvider: GoogleProviderProtocol? = nil, - facebookProvider: FacebookProviderProtocol? = nil, - phoneAuthProvider: PhoneAuthProviderProtocol? = nil) { + googleProvider: (any GoogleProviderProtocol)? = nil, + facebookProvider: (any FacebookProviderProtocol)? = nil, + phoneAuthProvider: (any PhoneAuthProviderProtocol)? = nil) { self.auth = auth self.configuration = configuration self.googleProvider = googleProvider @@ -87,14 +88,14 @@ public final class AuthService { public var errorMessage = "" public let passwordPrompt: PasswordPromptCoordinator = .init() - public var googleProvider: GoogleProviderProtocol? - public var facebookProvider: FacebookProviderProtocol? - public var phoneAuthProvider: PhoneAuthProviderProtocol? + public var googleProvider: (any GoogleProviderProtocol)? + public var facebookProvider: (any FacebookProviderProtocol)? + public var phoneAuthProvider: (any PhoneAuthProviderProtocol)? private var listenerManager: AuthListenerManager? private var signedInCredential: AuthCredential? - private var safeGoogleProvider: GoogleProviderProtocol { + private var safeGoogleProvider: any GoogleProviderProtocol { get throws { guard let provider = googleProvider else { throw AuthServiceError @@ -104,7 +105,7 @@ public final class AuthService { } } - private var safeFacebookProvider: FacebookProviderProtocol { + private var safeFacebookProvider: any FacebookProviderProtocol { get throws { guard let provider = facebookProvider else { throw AuthServiceError @@ -114,7 +115,7 @@ public final class AuthService { } } - private var safePhoneAuthProvider: PhoneAuthProviderProtocol { + private var safePhoneAuthProvider: any PhoneAuthProviderProtocol { get throws { guard let provider = phoneAuthProvider else { throw AuthServiceError diff --git a/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift b/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift index b47b06c44c..a7b3b503bd 100644 --- a/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift +++ b/FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift @@ -35,7 +35,7 @@ public class FacebookProviderSwift: FacebookProviderProtocol { shaNonce = CommonUtils.sha256Hash(of: rawNonce) } - @MainActor public var authButton: any View { + @MainActor public var authButton: SignInWithFacebookButton { return SignInWithFacebookButton() } diff --git a/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift b/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift index b9c342619c..eb74b1a534 100644 --- a/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift +++ b/FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/GoogleProviderSwift.swift @@ -25,7 +25,7 @@ public class GoogleProviderSwift: @preconcurrency GoogleProviderProtocol { self.clientID = clientID } - @MainActor public var authButton: any View { + @MainActor public var authButton: GoogleSignInButton { return GoogleSignInButton { Task { try await self.signInWithGoogle(clientID: self.clientID) diff --git a/FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderSwift.swift b/FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderSwift.swift index 9774c0319a..28bb2ba5ad 100644 --- a/FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderSwift.swift +++ b/FirebaseSwiftUI/FirebasePhoneAuthSwiftUI/Sources/Services/PhoneAuthProviderSwift.swift @@ -6,7 +6,7 @@ public typealias VerificationID = String public class PhoneAuthProviderSwift: @preconcurrency PhoneAuthProviderProtocol { - public var authButton: any View { + public var authButton: Button { // TODO: implement me return Button("Phone", action: { }) }