Skip to content

Commit a0a1435

Browse files
refactor: cleaner and simpler reauth implementation
1 parent edb8b73 commit a0a1435

File tree

2 files changed

+13
-79
lines changed

2 files changed

+13
-79
lines changed

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

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,68 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
@preconcurrency import FirebaseAuth
1615
import Observation
1716

18-
@MainActor
19-
protocol EmailPasswordOperationReauthentication {
20-
var passwordPrompt: PasswordPromptCoordinator { get }
21-
}
22-
23-
extension EmailPasswordOperationReauthentication {
24-
func reauthenticate() async throws {
25-
guard let user = Auth.auth().currentUser else {
26-
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")
27-
}
28-
29-
guard let email = user.email else {
30-
throw AuthServiceError.invalidCredentials("User does not have an email address")
31-
}
32-
33-
do {
34-
let password = try await passwordPrompt.confirmPassword()
35-
36-
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
37-
_ = try await Auth.auth().currentUser?.reauthenticate(with: credential)
38-
} catch {
39-
throw AuthServiceError.signInFailed(underlying: error)
40-
}
41-
}
42-
}
43-
44-
@MainActor
45-
class EmailPasswordDeleteUserOperation: AuthenticatedOperation,
46-
EmailPasswordOperationReauthentication {
47-
let passwordPrompt: PasswordPromptCoordinator
48-
49-
init(passwordPrompt: PasswordPromptCoordinator) {
50-
self.passwordPrompt = passwordPrompt
51-
}
52-
53-
func callAsFunction(on user: User) async throws {
54-
try await callAsFunction(on: user) {
55-
try await user.delete()
56-
}
57-
}
58-
}
59-
60-
class EmailPasswordUpdatePasswordOperation: AuthenticatedOperation,
61-
EmailPasswordOperationReauthentication {
62-
let passwordPrompt: PasswordPromptCoordinator
63-
let newPassword: String
64-
65-
init(passwordPrompt: PasswordPromptCoordinator, newPassword: String) {
66-
self.passwordPrompt = passwordPrompt
67-
self.newPassword = newPassword
68-
}
69-
70-
func callAsFunction(on user: User) async throws {
71-
try await callAsFunction(on: user) {
72-
try await user.updatePassword(to: newPassword)
73-
}
74-
}
75-
}
76-
17+
/// Coordinator for prompting users to enter their password during reauthentication flows
7718
@MainActor
7819
@Observable
7920
public final class PasswordPromptCoordinator {

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,12 @@ public final class AuthService {
283283
public extension AuthService {
284284
func deleteUser() async throws {
285285
do {
286-
if let user = auth.currentUser, let providerId = signedInCredential?.provider {
287-
if providerId == EmailAuthProviderID {
288-
let operation = EmailPasswordDeleteUserOperation(passwordPrompt: passwordPrompt)
289-
try await operation(on: user)
290-
} else {
291-
// Find provider by matching ID
292-
guard let matchingProvider = providers.first(where: { $0.id == providerId }) else {
293-
throw AuthServiceError.providerNotFound("No provider found for \(providerId)")
294-
}
295-
296-
try await matchingProvider.provider.deleteUser(user: user)
297-
}
286+
guard let user = auth.currentUser else {
287+
throw AuthServiceError.noCurrentUser
288+
}
289+
290+
try await withReauthenticationIfNeeded(on: user) {
291+
try await user.delete()
298292
}
299293
} catch {
300294
updateError(message: string.localizedErrorMessage(for: error))
@@ -304,14 +298,13 @@ public extension AuthService {
304298

305299
func updatePassword(to password: String) async throws {
306300
do {
307-
if let user = auth.currentUser {
308-
let operation = EmailPasswordUpdatePasswordOperation(
309-
passwordPrompt: passwordPrompt,
310-
newPassword: password
311-
)
312-
try await operation(on: user)
301+
guard let user = auth.currentUser else {
302+
throw AuthServiceError.noCurrentUser
303+
}
304+
305+
try await withReauthenticationIfNeeded(on: user) {
306+
try await user.updatePassword(to: password)
313307
}
314-
315308
} catch {
316309
updateError(message: string.localizedErrorMessage(for: error))
317310
throw error

0 commit comments

Comments
 (0)