@@ -8,33 +8,37 @@ import FirebaseCore
8
8
import CxxShim
9
9
import Foundation
10
10
11
- public typealias Auth = UnsafeMutablePointer < firebase . auth . Auth >
12
-
13
11
@available ( * , unavailable)
14
12
public enum AuthAPNSTokenType : Int {
15
13
case FIRAuthAPNSTokenTypeUnknown
16
14
case FIRAuthAPNSTokenTypeSandbox
17
15
case FIRAuthAPNSTokenTypeProd
18
16
}
19
17
20
- extension Auth {
18
+ public final class Auth {
19
+ let impl : UnsafeMutablePointer < firebase . auth . Auth >
20
+
21
+ init ( _ impl: UnsafeMutablePointer < firebase . auth . Auth > ) {
22
+ self . impl = impl
23
+ }
24
+
21
25
public var app : FirebaseApp ? {
22
- self . pointee. __appUnsafe ( )
26
+ impl . pointee. __appUnsafe ( )
23
27
}
24
28
25
29
public var currentUser : User ? {
26
- let user = self . pointee. current_user ( )
30
+ let user = impl . pointee. current_user ( )
27
31
guard user. is_valid ( ) else { return nil }
28
32
return user
29
33
}
30
34
31
35
public var languageCode : String ? {
32
36
get {
33
- let code = String ( self . pointee. language_code ( ) )
37
+ let code = String ( impl . pointee. language_code ( ) )
34
38
guard !code. isEmpty else { return nil }
35
39
return String ( code)
36
40
}
37
- set { self . pointee. set_language_code ( newValue) }
41
+ set { impl . pointee. set_language_code ( newValue) }
38
42
}
39
43
40
44
// @available(*, unavailable)
@@ -61,67 +65,91 @@ extension Auth {
61
65
}
62
66
63
67
public static func auth( app: FirebaseApp ) -> Auth {
64
- firebase. auth. Auth. GetAuth ( app, nil )
68
+ . init( firebase. auth. Auth. GetAuth ( app, nil ) )
69
+ }
70
+
71
+ public func updateCurrentUser( _ user: User , completion: ( ( Error ? ) -> Void ) ? ) {
72
+ fatalError ( " \( #function) not yet implemented " )
65
73
}
66
74
67
75
public func updateCurrentUser( _ user: User ) async throws {
68
76
fatalError ( " \( #function) not yet implemented " )
69
77
}
70
78
79
+ public func fetchSignInMethods( forEmail email: String , completion: ( ( [ String ] ? , Error ? ) -> Void ) ? ) {
80
+ fetchSignInMethodsImpl ( forEmail: email) { providers, error in
81
+ if let completion {
82
+ DispatchQueue . main. async {
83
+ completion ( providers, error)
84
+ }
85
+ }
86
+ }
87
+ }
88
+
71
89
public func fetchSignInMethods( forEmail email: String ) async throws
72
90
-> [ String ] {
73
- typealias Promise = CheckedContinuation < UnsafeMutablePointer < firebase . auth . Auth . FetchProvidersResult > , any Error >
74
- let providers = try await withCheckedThrowingContinuation { ( continuation: Promise ) in
75
- let future = self . pointee. FetchProvidersForEmail ( email)
76
- withUnsafePointer ( to: continuation) { continuation in
77
- future. OnCompletion_SwiftWorkaround ( { future, pvContinuation in
78
- let pContinuation = pvContinuation? . assumingMemoryBound ( to: Promise . self)
79
- if future. pointee. error ( ) == 0 {
80
- pContinuation. pointee. resume ( returning: . init( mutating: future. pointee. __resultUnsafe ( ) ) )
81
- } else {
82
- let code = future. pointee. error ( )
83
- let message = String ( cString: future. pointee. __error_messageUnsafe ( ) !)
84
- pContinuation. pointee. resume ( throwing: FirebaseError ( code: code, message: message) )
85
- }
86
- } , UnsafeMutableRawPointer ( mutating: continuation) )
91
+ try await withCheckedThrowingContinuation { continuation in
92
+ fetchSignInMethodsImpl ( forEmail: email) { providers, error in
93
+ if let error {
94
+ continuation. resume ( throwing: error)
95
+ } else {
96
+ continuation. resume ( returning: providers ?? [ ] )
97
+ }
87
98
}
88
- future. Wait ( firebase. FutureBase. kWaitTimeoutInfinite)
89
99
}
100
+ }
90
101
91
- #if SR70253
92
- // Workaround compiler crash (SR70253) by not using compactMap
93
- // which uses std.vector<std.string>::const_iterator.
94
- var result : [ String ] = [ ]
95
- for idx in 0 ..< providers. pointee. providers. size ( ) {
96
- result. append ( String ( providers. pointee. providers [ idx] ) )
102
+ private func fetchSignInMethodsImpl( forEmail email: String , completion: @escaping ( [ String ] ? , Error ? ) -> Void ) {
103
+ let future = swift_firebase. swift_cxx_shims. firebase. auth. auth_fetch_providers_for_email ( impl, email)
104
+ future. setCompletion ( {
105
+ let ( result, error) = future. resultAndError
106
+ var providers : [ String ] ?
107
+ if let result {
108
+ providers = result. providers. map ( String . init)
109
+ } else {
110
+ providers = nil
111
+ }
112
+ completion ( providers, error)
113
+ } )
114
+ }
115
+
116
+ public func signIn( withEmail email: String , password: String , completion: ( ( AuthDataResult ? , Error ? ) -> Void ) ? ) {
117
+ signInImpl ( withEmail: email, password: password) { data, error in
118
+ if let completion {
119
+ DispatchQueue . main. async {
120
+ completion ( data, error)
121
+ }
122
+ }
97
123
}
98
- return result
99
- #else
100
- return providers. pointee. providers. compactMap ( String . init)
101
- #endif
102
124
}
103
125
104
126
public func signIn( withEmail email: String , password: String ) async throws
105
127
-> AuthDataResult {
106
- typealias Promise = CheckedContinuation < AuthDataResult , any Error >
107
- return try await withCheckedThrowingContinuation { ( continuation: Promise ) in
108
- let future = self . pointee. SignInWithEmailAndPassword ( email, password)
109
- withUnsafePointer ( to: continuation) { continuation in
110
- future. OnCompletion_SwiftWorkaround ( { future, pvContinuation in
111
- let pContinuation = pvContinuation? . assumingMemoryBound ( to: Promise . self)
112
- if future. pointee. error ( ) == 0 {
113
- pContinuation. pointee. resume ( returning: . init( mutating: future. pointee. __resultUnsafe ( ) ) )
114
- } else {
115
- let code = future. pointee. error ( )
116
- let message = String ( cString: future. pointee. __error_messageUnsafe ( ) !)
117
- pContinuation. pointee. resume ( throwing: FirebaseError ( code: code, message: message) )
118
- }
119
- } , UnsafeMutableRawPointer ( mutating: continuation) )
128
+ try await withCheckedThrowingContinuation { continuation in
129
+ signInImpl ( withEmail: email, password: password) { data, error in
130
+ if let error {
131
+ continuation. resume ( throwing: error)
132
+ } else {
133
+ continuation. resume ( returning: data ?? . init( ) )
134
+ }
120
135
}
121
- future. Wait ( firebase. FutureBase. kWaitTimeoutInfinite)
122
136
}
123
137
}
124
138
139
+ private func signInImpl( withEmail email: String , password: String , completion: @escaping ( AuthDataResult ? , Error ? ) -> Void ) {
140
+ let future = swift_firebase. swift_cxx_shims. firebase. auth. auth_sign_in_with_email_and_password ( impl, email, password)
141
+ future. setCompletion ( {
142
+ let ( result, error) = future. resultAndError
143
+ var data : AuthDataResult ?
144
+ if let result {
145
+ data = . init( result)
146
+ } else {
147
+ data = nil
148
+ }
149
+ completion ( data, error)
150
+ } )
151
+ }
152
+
125
153
public func signIn( withEmail email: String , link: String ) async throws
126
154
-> AuthDataResult {
127
155
fatalError ( " \( #function) not yet implemented " )
@@ -143,27 +171,43 @@ extension Auth {
143
171
fatalError ( " \( #function) not yet implemented " )
144
172
}
145
173
174
+ public func createUser( withEmail email: String , password: String , completion: ( ( AuthDataResult ? , Error ? ) -> Void ) ? ) {
175
+ createUserImpl ( withEmail: email, password: password) { data, error in
176
+ if let completion {
177
+ DispatchQueue . main. async {
178
+ completion ( data, error)
179
+ }
180
+ }
181
+ }
182
+ }
183
+
146
184
public func createUser( withEmail email: String , password: String ) async throws
147
185
-> AuthDataResult {
148
- typealias Promise = CheckedContinuation < AuthDataResult , any Error >
149
- return try await withCheckedThrowingContinuation { ( continuation: Promise ) in
150
- let future = self . pointee. CreateUserWithEmailAndPassword ( email, password)
151
- withUnsafePointer ( to: continuation) { continuation in
152
- future. OnCompletion_SwiftWorkaround ( { future, pvContinuation in
153
- let pContinuation = pvContinuation? . assumingMemoryBound ( to: Promise . self)
154
- if future. pointee. error ( ) == 0 {
155
- pContinuation. pointee. resume ( returning: . init( mutating: future. pointee. __resultUnsafe ( ) ) )
156
- } else {
157
- let code = future. pointee. error ( )
158
- let message = String ( cString: future. pointee. __error_messageUnsafe ( ) !)
159
- pContinuation. pointee. resume ( throwing: FirebaseError ( code: code, message: message) )
160
- }
161
- } , UnsafeMutableRawPointer ( mutating: continuation) )
186
+ try await withCheckedThrowingContinuation { continuation in
187
+ createUserImpl ( withEmail: email, password: password) { data, error in
188
+ if let error {
189
+ continuation. resume ( throwing: error)
190
+ } else {
191
+ continuation. resume ( returning: data ?? . init( ) )
192
+ }
162
193
}
163
- future. Wait ( firebase. FutureBase. kWaitTimeoutInfinite)
164
194
}
165
195
}
166
196
197
+ private func createUserImpl( withEmail email: String , password: String , completion: @escaping ( AuthDataResult ? , Error ? ) -> Void ) {
198
+ let future = swift_firebase. swift_cxx_shims. firebase. auth. auth_create_user_with_email_and_password ( impl, email, password)
199
+ future. setCompletion ( {
200
+ let ( result, error) = future. resultAndError
201
+ var data : AuthDataResult ?
202
+ if let result {
203
+ data = . init( result)
204
+ } else {
205
+ data = nil
206
+ }
207
+ completion ( data, error)
208
+ } )
209
+ }
210
+
167
211
public func confirmPasswordReset( withCode code: String ,
168
212
newPassword: String ) async throws {
169
213
fatalError ( " \( #function) not yet implemented " )
@@ -181,26 +225,36 @@ extension Auth {
181
225
fatalError ( " \( #function) not yet implemented " )
182
226
}
183
227
228
+ public func sendPasswordReset( withEmail email: String , completion: ( ( Error ? ) -> Void ) ? ) {
229
+ sendPasswordResetImpl ( withEmail: email) { error in
230
+ if let completion {
231
+ DispatchQueue . main. async {
232
+ completion ( error)
233
+ }
234
+ }
235
+ }
236
+ }
237
+
184
238
public func sendPasswordReset( withEmail email: String ) async throws {
185
- typealias Promise = CheckedContinuation < Void , any Error >
186
- return try await withCheckedThrowingContinuation { ( continuation: Promise ) in
187
- let future = self . pointee. SendPasswordResetEmail ( email)
188
- withUnsafePointer ( to: continuation) { continuation in
189
- future. OnCompletion_SwiftWorkaround ( { future, pvContinuation in
190
- let pContinuation = pvContinuation? . assumingMemoryBound ( to: Promise . self)
191
- if future. pointee. error ( ) == 0 {
192
- pContinuation. pointee. resume ( )
193
- } else {
194
- let code = future. pointee. error ( )
195
- let message = String ( cString: future. pointee. __error_messageUnsafe ( ) !)
196
- pContinuation. pointee. resume ( throwing: FirebaseError ( code: code, message: message) )
197
- }
198
- } , UnsafeMutableRawPointer ( mutating: continuation) )
239
+ try await withCheckedThrowingContinuation { ( continuation: CheckedContinuation < Void , any Error > ) in
240
+ sendPasswordResetImpl ( withEmail: email) { error in
241
+ if let error {
242
+ continuation. resume ( throwing: error)
243
+ } else {
244
+ continuation. resume ( )
245
+ }
199
246
}
200
- future. Wait ( firebase. FutureBase. kWaitTimeoutInfinite)
201
247
}
202
248
}
203
249
250
+ private func sendPasswordResetImpl( withEmail email: String , completion: @escaping ( Error ? ) -> Void ) {
251
+ let future = swift_firebase. swift_cxx_shims. firebase. auth. auth_send_password_reset_email ( impl, email)
252
+ future. setCompletion ( {
253
+ let ( _, error) = future. resultAndError
254
+ completion ( error)
255
+ } )
256
+ }
257
+
204
258
// public func sendPasswordReset(withEmail email: String,
205
259
// actionCodeSettings: ActionCodeSettings) async throws {
206
260
// fatalError("\(#function) not yet implemented")
@@ -211,8 +265,9 @@ extension Auth {
211
265
// fatalError("\(#function) not yet implemented")
212
266
// }
213
267
214
- public func signOut( ) throws {
215
- self . pointee. SignOut ( )
268
+ @discardableResult public func signOut( ) throws -> Bool {
269
+ impl. pointee. SignOut ( )
270
+ return true
216
271
}
217
272
218
273
public func isSignIn( withEmailLink link: String ) -> Bool {
@@ -222,13 +277,13 @@ extension Auth {
222
277
public func addStateDidChangeListener( _ listener: @escaping ( Auth , User ? ) -> Void )
223
278
-> AuthStateDidChangeListenerHandle {
224
279
let handle = _AuthStateDidChangeListenerHandle ( listener)
225
- self . pointee. AddAuthStateListener ( handle. listener)
280
+ impl . pointee. AddAuthStateListener ( handle. listener)
226
281
return handle
227
282
}
228
283
229
284
public func removeStateDidChangeListener( _ listenerHandle: AuthStateDidChangeListenerHandle ) {
230
285
guard let handle = listenerHandle as? _AuthStateDidChangeListenerHandle else { return }
231
- self . pointee. RemoveAuthStateListener ( handle. listener)
286
+ impl . pointee. RemoveAuthStateListener ( handle. listener)
232
287
}
233
288
234
289
// public func addIDTokenDidChangeListener(_ listener: @escaping (Auth, User?) -> Void)
0 commit comments