-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathSignInWithFacebookButton.swift
More file actions
117 lines (110 loc) · 3.1 KB
/
SignInWithFacebookButton.swift
File metadata and controls
117 lines (110 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import AppTrackingTransparency
import FacebookCore
import FacebookLogin
import FirebaseAuth
import FirebaseAuthSwiftUI
import FirebaseCore
import SwiftUI
@MainActor
public struct SignInWithFacebookButton {
@Environment(AuthService.self) private var authService
@State private var errorMessage = ""
@State private var showCanceledAlert = false
@State private var limitedLogin = true
@State private var showUserTrackingAlert = false
@State private var trackingAuthorizationStatus: ATTrackingManager
.AuthorizationStatus = .notDetermined
public init() {
_trackingAuthorizationStatus = State(initialValue: ATTrackingManager
.trackingAuthorizationStatus)
}
private var limitedLoginBinding: Binding<Bool> {
Binding(
get: { self.limitedLogin },
set: { newValue in
if trackingAuthorizationStatus == .authorized {
self.limitedLogin = newValue
} else {
self.limitedLogin = false
}
}
)
}
func requestTrackingPermission() {
ATTrackingManager.requestTrackingAuthorization { status in
Task { @MainActor in
trackingAuthorizationStatus = status
if status != .authorized {
showUserTrackingAlert = true
}
}
}
}
}
extension SignInWithFacebookButton: View {
public var body: some View {
Button(action: {
Task {
do {
try await authService.signInWithFacebook(limitedLogin: limitedLogin)
} catch {
switch error {
case FacebookProviderError.signInCancelled:
showCanceledAlert = true
default:
errorMessage = authService.string.localizedErrorMessage(for: error)
}
}
}
}) {
HStack {
Image(systemName: "f.circle.fill")
.font(.title)
.foregroundColor(.white)
Text("Continue with Facebook")
.fontWeight(.semibold)
.foregroundColor(.white)
}
.padding()
.frame(maxWidth: .infinity)
.background(Color.blue)
.cornerRadius(8)
}
.alert(isPresented: $showCanceledAlert) {
Alert(
title: Text("Facebook login cancelled"),
dismissButton: .default(Text("OK"))
)
}
HStack {
Text("Authorize User Tracking")
.font(.footnote)
.foregroundColor(.blue)
.underline()
.onTapGesture {
requestTrackingPermission()
}
Toggle(isOn: limitedLoginBinding) {
HStack {
Spacer() // This will push the text to the left of the toggle
Text("Limited Login")
.foregroundColor(.blue)
}
}
.toggleStyle(SwitchToggleStyle(tint: .green))
.alert(isPresented: $showUserTrackingAlert) {
Alert(
title: Text("Authorize User Tracking"),
message: Text("For classic Facebook login, please authorize user tracking."),
dismissButton: .default(Text("OK"))
)
}
}
Text(errorMessage).foregroundColor(.red)
}
}
#Preview {
FirebaseOptions.dummyConfigurationForPreview()
return SignInWithFacebookButton()
.environment(AuthService())
}