Skip to content

Commit db2910d

Browse files
facebook auth add base + add to example project
1 parent 21423e4 commit db2910d

5 files changed

Lines changed: 82 additions & 2 deletions

File tree

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public protocol GoogleProviderProtocol {
66
@MainActor func signInWithGoogle(clientID: String) async throws -> AuthCredential
77
}
88

9+
public protocol FacebookProviderProtocol {}
10+
911
public enum AuthenticationProvider {
1012
case email
1113
case google
@@ -61,13 +63,16 @@ public final class AuthService {
6163
public let auth: Auth
6264
private var listenerManager: AuthListenerManager?
6365
private let googleProvider: GoogleProviderProtocol?
66+
private let facebookProvider: FacebookProviderProtocol?
6467
public let string: StringUtils
6568

6669
public init(configuration: AuthConfiguration = AuthConfiguration(), auth: Auth = Auth.auth(),
67-
googleProvider: GoogleProviderProtocol? = nil) {
70+
googleProvider: GoogleProviderProtocol? = nil,
71+
facebookProvider: FacebookProviderProtocol? = nil) {
6872
self.auth = auth
6973
self.configuration = configuration
7074
self.googleProvider = googleProvider
75+
self.facebookProvider = facebookProvider
7176
string = StringUtils(bundle: configuration.customStringsBundle ?? Bundle.module)
7277
listenerManager = AuthListenerManager(auth: auth, authEnvironment: self)
7378
}
@@ -91,6 +96,21 @@ public final class AuthService {
9196
}
9297
}
9398

99+
private var safeFacebookProvider: FacebookProviderProtocol {
100+
get throws {
101+
guard let provider = facebookProvider else {
102+
throw NSError(
103+
domain: "AuthEnvironmentErrorDomain",
104+
code: 1,
105+
userInfo: [
106+
NSLocalizedDescriptionKey: "`FacebookProviderSwift` has not been configured",
107+
]
108+
)
109+
}
110+
return provider
111+
}
112+
}
113+
94114
func updateAuthenticationState() {
95115
authenticationState =
96116
(currentUser == nil || currentUser?.isAnonymous == true)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @preconcurrency import FirebaseAuth
2+
import FirebaseAuthSwiftUI
3+
4+
public enum FacebookLoginType {
5+
case classic
6+
case limitedLogin
7+
}
8+
9+
public class FacebookProviderSwift: FacebookProviderProtocol {
10+
let shortName = "Facebook"
11+
let providerId = "facebook.com"
12+
var loginType: FacebookLoginType
13+
public init(loginType: FacebookLoginType = FacebookLoginType.classic) {
14+
self.loginType = loginType
15+
}
16+
17+
public func signInWithFacebook() {}
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import FirebaseAuthSwiftUI
2+
import SwiftUI
3+
4+
@MainActor
5+
public struct FacebookButtonView {
6+
@Environment(AuthService.self) private var authService
7+
@State private var errorMessage = ""
8+
9+
public init() {}
10+
11+
private func signInWithFacebook() async {}
12+
}
13+
14+
extension FacebookButtonView: View {
15+
public var body: some View {
16+
Button(action: {
17+
Task {
18+
try await signInWithFacebook()
19+
}
20+
}) {
21+
if authService.authenticationState != .authenticating {
22+
Text(authService.authenticationFlow == .login ? "Login with Google" : "Sign-up with Google")
23+
.padding(.vertical, 8)
24+
.frame(maxWidth: .infinity)
25+
} else {
26+
ProgressView()
27+
.progressViewStyle(CircularProgressViewStyle(tint: .white))
28+
.padding(.vertical, 8)
29+
.frame(maxWidth: .infinity)
30+
}
31+
}
32+
Text(errorMessage).foregroundColor(.red)
33+
}
34+
}

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ let package = Package(
283283
),
284284
.target(
285285
name: "FirebaseFacebookSwiftUI",
286-
path: "FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources",
287286
dependencies: [
288287
"FirebaseAuthSwiftUI",
289288
.product(name: "FacebookLogin", package: "Facebook"),
290289
],
290+
path: "FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources"
291291
),
292292
.testTarget(
293293
name: "FirebaseFacebookSwiftUITests",

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExample.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
4607CC9C2D9BFE29009EC3F5 /* FirebaseAuthSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 4607CC9B2D9BFE29009EC3F5 /* FirebaseAuthSwiftUI */; };
1111
4607CC9E2D9BFE29009EC3F5 /* FirebaseGoogleSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 4607CC9D2D9BFE29009EC3F5 /* FirebaseGoogleSwiftUI */; };
12+
4670DEA72D9EA9E100E0D36A /* FirebaseFacebookSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 4670DEA62D9EA9E100E0D36A /* FirebaseFacebookSwiftUI */; };
1213
46CB7B252D773F2100F1FD0A /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 46CB7B242D773F2100F1FD0A /* GoogleService-Info.plist */; };
1314
46F89C392D64B04E000F8BC0 /* FirebaseAuthSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 46F89C382D64B04E000F8BC0 /* FirebaseAuthSwiftUI */; };
1415
46F89C4D2D64BB9B000F8BC0 /* FirebaseAuthSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 46F89C4C2D64BB9B000F8BC0 /* FirebaseAuthSwiftUI */; };
@@ -74,6 +75,7 @@
7475
isa = PBXFrameworksBuildPhase;
7576
buildActionMask = 2147483647;
7677
files = (
78+
4670DEA72D9EA9E100E0D36A /* FirebaseFacebookSwiftUI in Frameworks */,
7779
46F89C392D64B04E000F8BC0 /* FirebaseAuthSwiftUI in Frameworks */,
7880
46F89C4D2D64BB9B000F8BC0 /* FirebaseAuthSwiftUI in Frameworks */,
7981
4607CC9E2D9BFE29009EC3F5 /* FirebaseGoogleSwiftUI in Frameworks */,
@@ -151,6 +153,7 @@
151153
46F89C4C2D64BB9B000F8BC0 /* FirebaseAuthSwiftUI */,
152154
4607CC9B2D9BFE29009EC3F5 /* FirebaseAuthSwiftUI */,
153155
4607CC9D2D9BFE29009EC3F5 /* FirebaseGoogleSwiftUI */,
156+
4670DEA62D9EA9E100E0D36A /* FirebaseFacebookSwiftUI */,
154157
);
155158
productName = FirebaseSwiftUIExample;
156159
productReference = 46F89C082D64A86C000F8BC0 /* FirebaseSwiftUIExample.app */;
@@ -622,6 +625,11 @@
622625
isa = XCSwiftPackageProductDependency;
623626
productName = FirebaseGoogleSwiftUI;
624627
};
628+
4670DEA62D9EA9E100E0D36A /* FirebaseFacebookSwiftUI */ = {
629+
isa = XCSwiftPackageProductDependency;
630+
package = 4607CC9A2D9BFE29009EC3F5 /* XCLocalSwiftPackageReference "../../../../firebaseUI-ios" */;
631+
productName = FirebaseFacebookSwiftUI;
632+
};
625633
46F89C382D64B04E000F8BC0 /* FirebaseAuthSwiftUI */ = {
626634
isa = XCSwiftPackageProductDependency;
627635
productName = FirebaseAuthSwiftUI;

0 commit comments

Comments
 (0)