-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathAccountService+Email.swift
More file actions
91 lines (74 loc) · 2.46 KB
/
AccountService+Email.swift
File metadata and controls
91 lines (74 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@preconcurrency import FirebaseAuth
import Observation
protocol EmailPasswordOperationReauthentication {
var passwordPrompt: PasswordPromptCoordinator { get }
}
extension EmailPasswordOperationReauthentication {
func reauthenticate() async throws -> AuthenticationToken {
guard let user = Auth.auth().currentUser else {
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")
}
guard let email = user.email else {
throw AuthServiceError.invalidCredentials("User does not have an email address")
}
do {
let password = try await passwordPrompt.confirmPassword()
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
try await Auth.auth().currentUser?.reauthenticate(with: credential)
return .firebase("")
} catch {
throw AuthServiceError.signInFailed(underlying: error)
}
}
}
class EmailPasswordDeleteUserOperation: AuthenticatedOperation,
EmailPasswordOperationReauthentication {
let passwordPrompt: PasswordPromptCoordinator
init(passwordPrompt: PasswordPromptCoordinator) {
self.passwordPrompt = passwordPrompt
}
func callAsFunction(on user: User) async throws {
try await callAsFunction(on: user) {
try await user.delete()
}
}
}
class EmailPasswordUpdatePasswordOperation: AuthenticatedOperation,
EmailPasswordOperationReauthentication {
let passwordPrompt: PasswordPromptCoordinator
let newPassword: String
init(passwordPrompt: PasswordPromptCoordinator, newPassword: String) {
self.passwordPrompt = passwordPrompt
self.newPassword = newPassword
}
func callAsFunction(on user: User) async throws {
try await callAsFunction(on: user) {
try await user.updatePassword(to: newPassword)
}
}
}
@MainActor
@Observable
public final class PasswordPromptCoordinator {
var isPromptingPassword = false
private var continuation: CheckedContinuation<String, Error>?
func confirmPassword() async throws -> String {
return try await withCheckedThrowingContinuation { continuation in
self.continuation = continuation
self.isPromptingPassword = true
}
}
func submit(password: String) {
continuation?.resume(returning: password)
cleanup()
}
func cancel() {
continuation?
.resume(throwing: AuthServiceError.reauthenticationRequired("Password entry cancelled"))
cleanup()
}
private func cleanup() {
continuation = nil
isPromptingPassword = false
}
}