@@ -35,13 +35,6 @@ public enum AuthView {
3535 case emailLink
3636}
3737
38- public enum AuthServiceError : Error {
39- case invalidEmailLink( String )
40- case notConfiguredProvider( String )
41- case clientIdNotFound( String )
42- case notConfiguredActionCodeSettings( String )
43- }
44-
4538@MainActor
4639private final class AuthListenerManager {
4740 private var authStateHandle : AuthStateDidChangeListenerHandle ?
@@ -93,6 +86,7 @@ public final class AuthService {
9386 public var authenticationState : AuthenticationState = . unauthenticated
9487 public var authenticationFlow : AuthenticationFlow = . login
9588 public var errorMessage = " "
89+ public let passwordPrompt : PasswordPromptCoordinator = . init( )
9690
9791 public var googleProvider : GoogleProviderProtocol ?
9892 public var facebookProvider : FacebookProviderProtocol ?
@@ -136,9 +130,7 @@ public final class AuthService {
136130 guard let actionCodeSettings = configuration
137131 . emailLinkSignInActionCodeSettings else {
138132 throw AuthServiceError
139- . notConfiguredActionCodeSettings (
140- " ActionCodeSettings has not been configured for `AuthConfiguration.emailLinkSignInActionCodeSettings` "
141- )
133+ . notConfiguredActionCodeSettings
142134 }
143135 return actionCodeSettings
144136 }
@@ -188,7 +180,6 @@ public final class AuthService {
188180 } else {
189181 do {
190182 try await auth. signIn ( with: credentials)
191- signedInCredential = credentials
192183 updateAuthenticationState ( )
193184 } catch {
194185 authenticationState = . unauthenticated
@@ -217,32 +208,14 @@ public final class AuthService {
217208
218209// MARK: - User API
219210
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-
230211public 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-
237212 func deleteUser( ) async throws {
238213 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)
245217 }
218+
246219 } catch {
247220 errorMessage = string. localizedErrorMessage (
248221 for: error
@@ -265,8 +238,6 @@ public extension AuthService {
265238
266239 do {
267240 try await auth. createUser ( withEmail: email, password: password)
268- let credential = EmailAuthProvider . credential ( withEmail: email, password: password)
269- signedInCredential = credential
270241 updateAuthenticationState ( )
271242 } catch {
272243 authenticationState = . unauthenticated
@@ -310,9 +281,7 @@ public extension AuthService {
310281 func handleSignInLink( url url: URL ) async throws {
311282 do {
312283 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
316285 }
317286 let link = url. absoluteString
318287 if auth. isSignIn ( withEmailLink: link) {
@@ -333,45 +302,25 @@ public extension AuthService {
333302
334303public extension AuthService {
335304 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+ )
354310 }
311+ let credential = try await safeGoogleProvider. signInWithGoogle ( clientID: clientID)
312+
313+ try await signIn ( credentials: credential)
355314 }
356315}
357316
358317// MARK: - Facebook Sign In
359318
360319public extension AuthService {
361320 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)
375324 }
376325}
377326
@@ -390,18 +339,8 @@ public extension AuthService {
390339 }
391340
392341 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)
406345 }
407346}
0 commit comments