@@ -35,13 +35,6 @@ public enum AuthView {
35
35
case emailLink
36
36
}
37
37
38
- public enum AuthServiceError : Error {
39
- case invalidEmailLink( String )
40
- case notConfiguredProvider( String )
41
- case clientIdNotFound( String )
42
- case notConfiguredActionCodeSettings( String )
43
- }
44
-
45
38
@MainActor
46
39
private final class AuthListenerManager {
47
40
private var authStateHandle : AuthStateDidChangeListenerHandle ?
@@ -93,6 +86,7 @@ public final class AuthService {
93
86
public var authenticationState : AuthenticationState = . unauthenticated
94
87
public var authenticationFlow : AuthenticationFlow = . login
95
88
public var errorMessage = " "
89
+ public let passwordPrompt : PasswordPromptCoordinator = . init( )
96
90
97
91
public var googleProvider : GoogleProviderProtocol ?
98
92
public var facebookProvider : FacebookProviderProtocol ?
@@ -136,9 +130,7 @@ public final class AuthService {
136
130
guard let actionCodeSettings = configuration
137
131
. emailLinkSignInActionCodeSettings else {
138
132
throw AuthServiceError
139
- . notConfiguredActionCodeSettings (
140
- " ActionCodeSettings has not been configured for `AuthConfiguration.emailLinkSignInActionCodeSettings` "
141
- )
133
+ . notConfiguredActionCodeSettings
142
134
}
143
135
return actionCodeSettings
144
136
}
@@ -188,7 +180,6 @@ public final class AuthService {
188
180
} else {
189
181
do {
190
182
try await auth. signIn ( with: credentials)
191
- signedInCredential = credentials
192
183
updateAuthenticationState ( )
193
184
} catch {
194
185
authenticationState = . unauthenticated
@@ -217,32 +208,14 @@ public final class AuthService {
217
208
218
209
// MARK: - User API
219
210
220
- extension Date {
221
- func isWithinPast( minutes: Int ) -> Bool {
222
- let calendar = Calendar . current
223
- guard let timeAgo = calendar. date ( byAdding: . minute, value: - minutes, to: Date ( ) ) else {
224
- return false
225
- }
226
- return self >= timeAgo && self <= Date ( )
227
- }
228
- }
229
-
230
211
public extension AuthService {
231
- func reauthenticate( ) async throws {
232
- if let user = auth. currentUser, let credential = signedInCredential {
233
- try await user. reauthenticate ( with: credential)
234
- }
235
- }
236
-
237
212
func deleteUser( ) async throws {
238
213
do {
239
- if let user = auth. currentUser, let lastSignInDate = user. metadata. lastSignInDate {
240
- let needsReauth = !lastSignInDate. isWithinPast ( minutes: 5 )
241
- if needsReauth {
242
- try await reauthenticate ( )
243
- }
244
- try await user. delete ( )
214
+ if let user = auth. currentUser {
215
+ let operation = EmailPasswordDeleteUserOperation ( passwordPrompt: passwordPrompt)
216
+ try await operation ( on: user)
245
217
}
218
+
246
219
} catch {
247
220
errorMessage = string. localizedErrorMessage (
248
221
for: error
@@ -265,8 +238,6 @@ public extension AuthService {
265
238
266
239
do {
267
240
try await auth. createUser ( withEmail: email, password: password)
268
- let credential = EmailAuthProvider . credential ( withEmail: email, password: password)
269
- signedInCredential = credential
270
241
updateAuthenticationState ( )
271
242
} catch {
272
243
authenticationState = . unauthenticated
@@ -310,9 +281,7 @@ public extension AuthService {
310
281
func handleSignInLink( url url: URL ) async throws {
311
282
do {
312
283
guard let email = emailLink else {
313
- throw AuthServiceError . invalidEmailLink (
314
- " Invalid sign in link. Most likely, the link you used has expired. Try signing in again. "
315
- )
284
+ throw AuthServiceError . invalidEmailLink
316
285
}
317
286
let link = url. absoluteString
318
287
if auth. isSignIn ( withEmailLink: link) {
@@ -333,45 +302,25 @@ public extension AuthService {
333
302
334
303
public extension AuthService {
335
304
func signInWithGoogle( ) async throws {
336
- authenticationState = . authenticating
337
- do {
338
- guard let clientID = auth. app? . options. clientID else {
339
- throw AuthServiceError
340
- . clientIdNotFound (
341
- " OAuth client ID not found. Please make sure Google Sign-In is enabled in the Firebase console. You may have to download a new GoogleService-Info.plist file after enabling Google Sign-In. "
342
- )
343
- }
344
- let credential = try await safeGoogleProvider. signInWithGoogle ( clientID: clientID)
345
-
346
- try await signIn ( credentials: credential)
347
- updateAuthenticationState ( )
348
- } catch {
349
- authenticationState = . unauthenticated
350
- errorMessage = string. localizedErrorMessage (
351
- for: error
352
- )
353
- throw error
305
+ guard let clientID = auth. app? . options. clientID else {
306
+ throw AuthServiceError
307
+ . clientIdNotFound (
308
+ " OAuth client ID not found. Please make sure Google Sign-In is enabled in the Firebase console. You may have to download a new GoogleService-Info.plist file after enabling Google Sign-In. "
309
+ )
354
310
}
311
+ let credential = try await safeGoogleProvider. signInWithGoogle ( clientID: clientID)
312
+
313
+ try await signIn ( credentials: credential)
355
314
}
356
315
}
357
316
358
317
// MARK: - Facebook Sign In
359
318
360
319
public extension AuthService {
361
320
func signInWithFacebook( limitedLogin: Bool = true ) async throws {
362
- authenticationState = . authenticating
363
- do {
364
- let credential = try await safeFacebookProvider
365
- . signInWithFacebook ( isLimitedLogin: limitedLogin)
366
- try await signIn ( credentials: credential)
367
- updateAuthenticationState ( )
368
- } catch {
369
- authenticationState = . unauthenticated
370
- errorMessage = string. localizedErrorMessage (
371
- for: error
372
- )
373
- throw error
374
- }
321
+ let credential = try await safeFacebookProvider
322
+ . signInWithFacebook ( isLimitedLogin: limitedLogin)
323
+ try await signIn ( credentials: credential)
375
324
}
376
325
}
377
326
@@ -390,18 +339,8 @@ public extension AuthService {
390
339
}
391
340
392
341
func signInWithPhoneNumber( verificationID: String , verificationCode: String ) async throws {
393
- authenticationState = . authenticating
394
- do {
395
- let credential = PhoneAuthProvider . provider ( )
396
- . credential ( withVerificationID: verificationID, verificationCode: verificationCode)
397
- try await signIn ( credentials: credential)
398
- updateAuthenticationState ( )
399
- } catch {
400
- authenticationState = . unauthenticated
401
- errorMessage = string. localizedErrorMessage (
402
- for: error
403
- )
404
- throw error
405
- }
342
+ let credential = PhoneAuthProvider . provider ( )
343
+ . credential ( withVerificationID: verificationID, verificationCode: verificationCode)
344
+ try await signIn ( credentials: credential)
406
345
}
407
346
}
0 commit comments