Account deletion

This commit is contained in:
2026-04-09 23:18:31 -05:00
parent f678e36584
commit ccaf0e179f
2 changed files with 46 additions and 0 deletions

View File

@@ -8,6 +8,8 @@ final class ProfileViewModel {
var isSaving = false
var isEditing = false
var errorMessage: String?
var showDeleteConfirm = false
var isDeleting = false
// Editable fields
var piFirstName = ""
@@ -49,6 +51,19 @@ final class ProfileViewModel {
}
}
func deleteAccount() async {
isDeleting = true
defer { isDeleting = false }
do {
try await authClient.deleteAccount()
await MainActor.run {
AppState.shared.signedOut()
}
} catch {
errorMessage = "Failed to delete account. Please try again."
}
}
func signOut() async {
try? await authClient.signOut()
await MainActor.run {
@@ -97,6 +112,21 @@ struct ProfileView: View {
}
}
Section {
Button(role: .destructive) {
viewModel.showDeleteConfirm = true
} label: {
HStack {
Spacer()
Text("Delete Account")
Spacer()
}
}
.disabled(viewModel.isDeleting)
} footer: {
Text("Permanently deletes your account and all data including chemicals and protocols.")
}
Section {
Button(role: .destructive) {
Task { await viewModel.signOut() }
@@ -142,6 +172,14 @@ struct ProfileView: View {
} message: {
Text(viewModel.errorMessage ?? "")
}
.alert("Delete Account?", isPresented: $viewModel.showDeleteConfirm) {
Button("Cancel", role: .cancel) { }
Button("Delete", role: .destructive) {
Task { await viewModel.deleteAccount() }
}
} message: {
Text("Are you sure? This will permanently delete your account and all your data. This action cannot be undone.")
}
}
.task {
await viewModel.load()

View File

@@ -200,6 +200,14 @@ public final class AuthClient: Sendable {
return user
}
// MARK: - Delete Account
/// Permanently delete the user's account and all associated data.
public func deleteAccount() async throws {
_ = try await api.postRaw("/api/account/delete", body: EmptyBody())
api.clearSessionCookies()
}
// MARK: - Sign Out
/// Sign out. Clears the session cookie.