Skip to content

Commit 1028c7b

Browse files
refactor: use the same reauthenticate method for providers with same logic
1 parent ad1f667 commit 1028c7b

File tree

7 files changed

+29
-67
lines changed

7 files changed

+29
-67
lines changed

FirebaseSwiftUI/FirebaseAppleSwiftUI/Sources/Services/AccountService+Apple.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,12 @@
99
import FirebaseAuthSwiftUI
1010
import Observation
1111

12-
protocol AppleOperationReauthentication {
12+
protocol AppleOperationReauthentication: ProviderOperationReauthentication {
1313
var appleProvider: AppleProviderSwift { get }
1414
}
1515

1616
extension AppleOperationReauthentication {
17-
@MainActor func reauthenticate() async throws {
18-
guard let user = Auth.auth().currentUser else {
19-
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")
20-
}
21-
22-
do {
23-
let credential = try await appleProvider.createAuthCredential()
24-
try await user.reauthenticate(with: credential)
25-
} catch {
26-
throw AuthServiceError.signInFailed(underlying: error)
27-
}
28-
}
17+
var authProvider: AuthProviderSwift { appleProvider }
2918
}
3019

3120
@MainActor

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AccountService+Email.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ protocol EmailPasswordOperationReauthentication {
2020
}
2121

2222
extension EmailPasswordOperationReauthentication {
23-
// TODO: - @MainActor because User is non-sendable. Might change this once User is sendable in firebase-ios-sdk
2423
@MainActor func reauthenticate() async throws {
2524
guard let user = Auth.auth().currentUser else {
2625
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AccountService.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,22 @@ public extension AuthenticatedOperation {
4545
}
4646
}
4747
}
48+
49+
public protocol ProviderOperationReauthentication {
50+
var authProvider: AuthProviderSwift { get }
51+
}
52+
53+
public extension ProviderOperationReauthentication {
54+
@MainActor func reauthenticate() async throws {
55+
guard let user = Auth.auth().currentUser else {
56+
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")
57+
}
58+
59+
do {
60+
let credential = try await authProvider.createAuthCredential()
61+
try await user.reauthenticate(with: credential)
62+
} catch {
63+
throw AuthServiceError.signInFailed(underlying: error)
64+
}
65+
}
66+
}

FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/AccountService+Facebook.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,12 @@
2323
import FirebaseAuthSwiftUI
2424
import Observation
2525

26-
protocol FacebookOperationReauthentication {
26+
protocol FacebookOperationReauthentication: ProviderOperationReauthentication {
2727
var facebookProvider: FacebookProviderSwift { get }
2828
}
2929

3030
extension FacebookOperationReauthentication {
31-
@MainActor func reauthenticate() async throws {
32-
guard let user = Auth.auth().currentUser else {
33-
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")
34-
}
35-
36-
do {
37-
let credential = try await facebookProvider.createAuthCredential()
38-
try await user.reauthenticate(with: credential)
39-
} catch {
40-
throw AuthServiceError.signInFailed(underlying: error)
41-
}
42-
}
31+
var authProvider: AuthProviderSwift { facebookProvider }
4332
}
4433

4534
@MainActor

FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Services/AccountService+Google.swift

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,12 @@
3030
import FirebaseAuthSwiftUI
3131
import Observation
3232

33-
protocol GoogleOperationReauthentication {
33+
protocol GoogleOperationReauthentication: ProviderOperationReauthentication {
3434
var googleProvider: GoogleProviderSwift { get }
3535
}
3636

3737
extension GoogleOperationReauthentication {
38-
@MainActor func reauthenticate() async throws {
39-
guard let user = Auth.auth().currentUser else {
40-
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")
41-
}
42-
43-
do {
44-
let credential = try await googleProvider.createAuthCredential()
45-
try await user.reauthenticate(with: credential)
46-
47-
} catch {
48-
throw AuthServiceError.signInFailed(underlying: error)
49-
}
50-
}
38+
var authProvider: AuthProviderSwift { googleProvider }
5139
}
5240

5341
@MainActor

FirebaseSwiftUI/FirebaseOAuthSwiftUI/Sources/Services/AccountService+OAuth.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,12 @@
99
import FirebaseAuthSwiftUI
1010
import Observation
1111

12-
protocol OAuthOperationReauthentication {
12+
protocol OAuthOperationReauthentication: ProviderOperationReauthentication {
1313
var oauthProvider: OAuthProviderSwift { get }
1414
}
1515

1616
extension OAuthOperationReauthentication {
17-
@MainActor func reauthenticate() async throws {
18-
guard let user = Auth.auth().currentUser else {
19-
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")
20-
}
21-
22-
do {
23-
let credential = try await oauthProvider.createAuthCredential()
24-
try await user.reauthenticate(with: credential)
25-
} catch {
26-
throw AuthServiceError.signInFailed(underlying: error)
27-
}
28-
}
17+
var authProvider: AuthProviderSwift { oauthProvider }
2918
}
3019

3120
@MainActor

FirebaseSwiftUI/FirebaseTwitterSwiftUI/Sources/Services/AccountService+Twitter.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,12 @@
99
import FirebaseAuthSwiftUI
1010
import Observation
1111

12-
protocol TwitterOperationReauthentication {
12+
protocol TwitterOperationReauthentication: ProviderOperationReauthentication {
1313
var twitterProvider: TwitterProviderSwift { get }
1414
}
1515

1616
extension TwitterOperationReauthentication {
17-
@MainActor func reauthenticate() async throws {
18-
guard let user = Auth.auth().currentUser else {
19-
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")
20-
}
21-
22-
do {
23-
let credential = try await twitterProvider.createAuthCredential()
24-
try await user.reauthenticate(with: credential)
25-
} catch {
26-
throw AuthServiceError.signInFailed(underlying: error)
27-
}
28-
}
17+
var authProvider: AuthProviderSwift { twitterProvider }
2918
}
3019

3120
@MainActor

0 commit comments

Comments
 (0)