Skip to content

Commit c50dbd1

Browse files
differentiate limitedLogin and classic login
1 parent 2a1f4e3 commit c50dbd1

4 files changed

Lines changed: 69 additions & 21 deletions

File tree

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public final class AuthService {
143143
}
144144
}
145145

146-
func signIn(with credentials: AuthCredential) async throws {
146+
public func signIn(with credentials: AuthCredential) async throws {
147147
authenticationState = .authenticating
148148
do {
149149
try await auth.signIn(with: credentials)

FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderSwift.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import FirebaseAuthSwiftUI
55
let kFacebookEmailScope = "email"
66
let kFacebookProfileScope = "public_profile"
77
let kDefaultFacebookScopes = [kFacebookEmailScope, kFacebookProfileScope]
8+
// TODO - need to think how to handle this
9+
let kFacebookProviderId = "facebook.com"
810

911
public enum FacebookLoginType {
1012
case classic

FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Utils/FacebookUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class FacebookUtils {
3131
return result
3232
}
3333

34-
func sha256Hash(of input: String) -> String {
34+
static func sha256Hash(of input: String) -> String {
3535
guard let data = input.data(using: .utf8) else { return "" }
3636
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
3737
data.withUnsafeBytes {

FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Views/FacebookButtonView.swift

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,66 @@
11
import FacebookCore
22
import FacebookLogin
3+
import FirebaseAuth
34
import FirebaseAuthSwiftUI
45
import SwiftUI
56

67
@MainActor
78
public struct FacebookButtonView {
89
@Environment(AuthService.self) private var authService
910
@State private var errorMessage = ""
10-
@State private var limitedLogin: Bool = true
11-
@State private var nonce: String? = FacebookUtils.randomNonce()
11+
@State private var limitedLogin: Bool = false
12+
@State private var rawNonce: String
13+
@State private var shaNonce: String
14+
15+
public init() {
16+
let nonce = FacebookUtils.randomNonce()
17+
_rawNonce = State(initialValue: nonce)
18+
_shaNonce = State(initialValue: FacebookUtils.sha256Hash(of: nonce))
19+
}
1220

13-
public init() {}
21+
private func classicLogin() async {
22+
do {
23+
if let token = AccessToken.current,
24+
!token.isExpired {
25+
let credential = FacebookAuthProvider
26+
.credential(withAccessToken: token.tokenString)
27+
try await authService.signIn(with: credential)
28+
} else {
29+
throw NSError(
30+
domain: "FacebookSwiftErrorDomain",
31+
code: 1,
32+
userInfo: [
33+
NSLocalizedDescriptionKey: "Access token has expired or not available. Please sign-in with Facebook before attempting to create a Facebook provider credential",
34+
]
35+
)
36+
}
37+
} catch {
38+
errorMessage = authService.string.localizedErrorMessage(
39+
for: error
40+
)
41+
}
42+
}
1443

15-
private func signInWithFacebook() async {
16-
if let token = AccessToken.current,
17-
!token.isExpired {
18-
// AuthenticationToken.current.
19-
// no need to login with Facebook, create credential and sign in here
44+
private func limitedLogin() async {
45+
do {
46+
if let idToken = AuthenticationToken.current {
47+
let credential = OAuthProvider.credential(withProviderID: kFacebookProviderId,
48+
idToken: idToken.tokenString,
49+
rawNonce: rawNonce)
50+
try await authService.signIn(with: credential)
51+
} else {
52+
throw NSError(
53+
domain: "FacebookSwiftErrorDomain",
54+
code: 2,
55+
userInfo: [
56+
NSLocalizedDescriptionKey: "Authentication is not available. Please sign-in with Facebook before attempting to create a Facebook provider credential",
57+
]
58+
)
59+
}
60+
} catch {
61+
errorMessage = authService.string.localizedErrorMessage(
62+
for: error
63+
)
2064
}
2165
}
2266
}
@@ -25,13 +69,17 @@ extension FacebookButtonView: View {
2569
public var body: some View {
2670
FacebookLoginButtonView(
2771
isLimitedLogin: $limitedLogin,
28-
nonce: $nonce,
72+
nonce: $shaNonce,
2973
onLoginResult: { error in
3074
Task {
3175
if let error = error {
3276
errorMessage = authService.string.localizedErrorMessage(for: error)
3377
} else {
34-
await signInWithFacebook()
78+
if limitedLogin {
79+
await limitedLogin()
80+
} else {
81+
await classicLogin()
82+
}
3583
}
3684
}
3785
}
@@ -44,8 +92,8 @@ struct FacebookLoginButtonView: UIViewRepresentable {
4492
typealias UIViewType = FBLoginButton
4593

4694
@Binding var isLimitedLogin: Bool
47-
@Binding var nonce: String?
48-
var onLoginResult: ((Error?) -> Void)?
95+
@Binding var nonce: String
96+
var onLoginResult: (Error?) -> Void
4997

5098
class Coordinator: NSObject, @preconcurrency LoginButtonDelegate {
5199
var parent: FacebookLoginButtonView
@@ -56,11 +104,9 @@ struct FacebookLoginButtonView: UIViewRepresentable {
56104

57105
@MainActor func loginButtonWillLogin(_ loginButton: FBLoginButton) -> Bool {
58106
loginButton.loginTracking = parent.isLimitedLogin ? .limited : .enabled
59-
loginButton.permissions = ["public_profile", "email"]
107+
// loginButton.permissions = ["public_profile", "email"]
60108

61-
if let nonce = parent.nonce, !nonce.isEmpty {
62-
loginButton.nonce = nonce
63-
}
109+
loginButton.nonce = parent.nonce
64110

65111
return true
66112
}
@@ -69,20 +115,20 @@ struct FacebookLoginButtonView: UIViewRepresentable {
69115
didCompleteWith result: LoginManagerLoginResult?,
70116
error: Error?) {
71117
if let error = error {
72-
parent.onLoginResult?(error)
118+
parent.onLoginResult(error)
73119
return
74120
}
75121

76122
guard let result = result, !result.isCancelled else {
77-
parent.onLoginResult?(NSError(
123+
parent.onLoginResult(NSError(
78124
domain: "FacebookLogin",
79125
code: 1,
80126
userInfo: [NSLocalizedDescriptionKey: "Login was cancelled."]
81127
))
82128
return
83129
}
84130

85-
parent.onLoginResult?(nil)
131+
parent.onLoginResult(nil)
86132
}
87133

88134
func loginButtonDidLogOut(_: FBLoginButton) {

0 commit comments

Comments
 (0)