@@ -8,10 +8,17 @@ import SwiftBoost
8
8
public class FirebaseWrapperAuth {
9
9
10
10
public static func configure( authDidChangedWork: ( ( ) -> Void ) ? = nil ) {
11
- debug ( " FirebaseWrapper: Auth configure " )
11
+ // Logs
12
+ debug ( " FirebaseWrapper: Auth configure. " )
13
+ debug ( " FirebaseWrapper: Current state isAuthed: " + ( isAuthed ? " true " : " false " ) )
14
+ if isAuthed {
15
+ debug ( " FirebaseWrapper: userID: \( userID ?? . empty) , email: \( userEmail ?? " nil " ) " )
16
+ }
17
+ // Observer Clean
12
18
if let observer = shared. observer {
13
19
Auth . auth ( ) . removeStateDidChangeListener ( observer)
14
20
}
21
+ // Configure Observer
15
22
shared. observer = Auth . auth ( ) . addStateDidChangeListener { auth, user in
16
23
let newState = isAuthed
17
24
let cachedState = isAuthedStored
@@ -23,7 +30,14 @@ public class FirebaseWrapperAuth {
23
30
}
24
31
25
32
public static func application( _ app: UIApplication , open url: URL , options: [ UIApplication . OpenURLOptionsKey : Any ] = [ : ] ) -> Bool {
26
- return GIDSignIn . sharedInstance. handle ( url)
33
+ let handleEmailWay = handleSignInWithEmailURL ( url) { error in
34
+ // Process auth after handle email
35
+ }
36
+ if handleEmailWay {
37
+ return true
38
+ } else {
39
+ return GIDSignIn . sharedInstance. handle ( url)
40
+ }
27
41
}
28
42
29
43
// MARK: - Data
@@ -33,6 +47,17 @@ public class FirebaseWrapperAuth {
33
47
public static var userName : String ? { Auth . auth ( ) . currentUser? . displayName }
34
48
public static var userEmail : String ? { Auth . auth ( ) . currentUser? . email }
35
49
50
+ public static var providers : [ FirebaseAuthProvider ] {
51
+ guard let providerData = Auth . auth ( ) . currentUser? . providerData else { return [ ] }
52
+ var providers : [ FirebaseAuthProvider ] = [ ]
53
+ for providerMeta in providerData {
54
+ if let provider = FirebaseAuthProvider . getByBaseURL ( providerMeta. providerID) {
55
+ providers. append ( provider)
56
+ }
57
+ }
58
+ return providers
59
+ }
60
+
36
61
private static var isAuthedStored : Bool {
37
62
get { UserDefaults . standard. bool ( forKey: " firebase_wrapper_auth_is_authed_stored " ) }
38
63
set { UserDefaults . standard. set ( newValue, forKey: " firebase_wrapper_auth_is_authed_stored " ) }
@@ -47,8 +72,12 @@ public class FirebaseWrapperAuth {
47
72
return
48
73
}
49
74
AppleAuthService . signIn ( on: window) { data, appleError in
75
+ if let appleError {
76
+ debug ( " FirebaseWrapper: Sign in with Apple error: \( appleError. localizedDescription) " )
77
+ return
78
+ }
50
79
guard let data else {
51
- completion ? ( nil , appleError )
80
+ completion ? ( nil , AuthError . cantMakeData )
52
81
return
53
82
}
54
83
let credential = OAuthProvider . appleCredential (
@@ -64,9 +93,13 @@ public class FirebaseWrapperAuth {
64
93
65
94
public static func signInWithGoogle( on controller: UIViewController , completion: ( ( SignInWithAppleData ? , Error ? ) -> Void ) ? ) {
66
95
debug ( " FirebaseWrapper: Auth start sign in with Google " )
67
- GoogleAuthService . signInWithGoogle ( on: controller) { data, googleError in
96
+ GoogleAuthService . signIn ( on: controller) { data, googleError in
97
+ if let googleError {
98
+ debug ( " FirebaseWrapper: Sign in with Google error: \( googleError. localizedDescription) " )
99
+ return
100
+ }
68
101
guard let data else {
69
- completion ? ( nil , googleError )
102
+ completion ? ( nil , AuthError . cantMakeData )
70
103
return
71
104
}
72
105
let credential = GoogleAuthProvider . credential ( withIDToken: data. identityToken, accessToken: data. accessToken)
@@ -76,6 +109,31 @@ public class FirebaseWrapperAuth {
76
109
}
77
110
}
78
111
112
+ public static func signInWithEmail( email: String , handleURL: URL , completion: ( ( Error ? ) -> Void ) ? ) {
113
+ debug ( " FirebaseWrapper: Auth start sign in with Email " )
114
+ EmailAuthService . signIn ( email: email, handleURL: handleURL) { emailError in
115
+ if let emailError {
116
+ debug ( " FirebaseWrapper: Sign in with Email error: \( emailError. localizedDescription) " )
117
+ }
118
+ completion ? ( emailError)
119
+ }
120
+ }
121
+
122
+ static func handleSignInWithEmailURL( _ url: URL , completion: ( ( Error ? ) -> Void ) ? ) -> Bool {
123
+ guard Auth . auth ( ) . isSignIn ( withEmailLink: url. absoluteString) else {
124
+ completion ? ( AuthError . cantMakeData)
125
+ return false
126
+ }
127
+ guard let processingEmail = EmailAuthService . processingEmail else {
128
+ completion ? ( AuthError . cantMakeData)
129
+ return false
130
+ }
131
+ Auth . auth ( ) . signIn ( withEmail: processingEmail, link: url. absoluteString) { user, emailError in
132
+ completion ? ( emailError)
133
+ }
134
+ return true
135
+ }
136
+
79
137
public static func signOut( completion: @escaping ( Error ? ) -> Void ) {
80
138
debug ( " FirebaseWrapper: Auth start sign out " )
81
139
do {
@@ -89,11 +147,42 @@ public class FirebaseWrapperAuth {
89
147
public static func delete( on controller: UIViewController , completion: @escaping ( Error ? ) -> Void ) {
90
148
debug ( " FirebaseWrapper: Auth start delete " )
91
149
150
+ // Basic Delete Account
151
+ let deleteAccountAction = {
152
+ do {
153
+ try await Auth . auth ( ) . currentUser? . delete ( )
154
+ DispatchQueue . main. async {
155
+ completion ( nil )
156
+ }
157
+ } catch {
158
+ DispatchQueue . main. async {
159
+ completion ( error)
160
+ }
161
+ }
162
+ }
163
+
164
+ let providers = providers
165
+ if providers. isEmpty {
166
+ #warning("del account error make enum")
167
+ completion ( AuthError . cantMakeData)
168
+ return
169
+ }
170
+
171
+ let provider = providers. first!
172
+
173
+ // Check which provider for reauth https://firebase.google.com/docs/auth/ios/manage-users#delete_a_user
174
+ // Check if sign in with apple for revoke token, maybe first do it and later check others
175
+ // after auth deelte account
176
+ // FIRAuthErrorCodeCredentialTooOld error when delete call reauth only when happen this error
177
+
178
+ /*
92
179
guard let providers = Auth.auth().currentUser?.providerData, !providers.isEmpty else {
93
180
completion(AuthError.cantMakeData)
94
181
return
95
182
}
96
183
184
+
185
+
97
186
// Basic Delete Account
98
187
let deleteAccountAction = {
99
188
do {
@@ -137,7 +226,7 @@ public class FirebaseWrapperAuth {
137
226
// Basic Delete Account
138
227
await deleteAccountAction()
139
228
}
140
- }
229
+ }*/
141
230
}
142
231
143
232
public static func setDisplayName( _ name: String , completion: @escaping ( Error ? ) -> Void ) {
0 commit comments