Let users delete their account from settings (#46)

This commit is contained in:
2026-11-04 19:59:12 +01:00
parent 49736d186c
commit 1012c74d60
4 changed files with 120 additions and 0 deletions
+18
View File
@@ -212,6 +212,24 @@ defmodule Chesstrainer.Accounts do
|> update_user_and_delete_all_tokens()
end
## Deletion
@doc """
Permanently deletes a user and all of their data.
Attempts and session tokens are removed by database cascades. There is no
soft delete and no way to recover the account.
## Examples
iex> delete_user(user)
{:ok, %User{}}
"""
def delete_user(%User{} = user) do
Repo.delete(user)
end
## Session
@doc """
@@ -65,6 +65,30 @@ defmodule ChesstrainerWeb.UserLive.Settings do
Save Password
</.button>
</.form>
<div class="divider" />
<section id="delete_account" class="space-y-4">
<.header>
Delete account
<:subtitle>This action is permanent and cannot be undone.</:subtitle>
</.header>
<p class="text-sm text-base-content/70">
Deleting your account permanently removes your profile, rating and training history.
Nothing can be recovered.
</p>
<button
id="delete_account_button"
type="button"
class="btn btn-error"
phx-click="delete_account"
data-confirm="This permanently deletes your account and all of your data. Nothing can be recovered. Are you sure?"
>
Delete my account
</button>
</section>
</Layouts.app>
"""
end
@@ -157,4 +181,16 @@ defmodule ChesstrainerWeb.UserLive.Settings do
{:noreply, assign(socket, password_form: to_form(changeset, action: :insert))}
end
end
def handle_event("delete_account", _params, socket) do
user = socket.assigns.current_scope.user
true = Accounts.sudo_mode?(user)
{:ok, _user} = Accounts.delete_user(user)
{:noreply,
socket
|> put_flash(:info, "Your account has been deleted.")
|> redirect(to: ~p"/")}
end
end