mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Let users delete their account from settings (#46)
This commit is contained in:
@@ -212,6 +212,24 @@ defmodule Chesstrainer.Accounts do
|
|||||||
|> update_user_and_delete_all_tokens()
|
|> update_user_and_delete_all_tokens()
|
||||||
end
|
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
|
## Session
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|||||||
@@ -65,6 +65,30 @@ defmodule ChesstrainerWeb.UserLive.Settings do
|
|||||||
Save Password
|
Save Password
|
||||||
</.button>
|
</.button>
|
||||||
</.form>
|
</.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>
|
</Layouts.app>
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
@@ -157,4 +181,16 @@ defmodule ChesstrainerWeb.UserLive.Settings do
|
|||||||
{:noreply, assign(socket, password_form: to_form(changeset, action: :insert))}
|
{:noreply, assign(socket, password_form: to_form(changeset, action: :insert))}
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@@ -4,7 +4,11 @@ defmodule Chesstrainer.AccountsTest do
|
|||||||
alias Chesstrainer.Accounts
|
alias Chesstrainer.Accounts
|
||||||
|
|
||||||
import Chesstrainer.AccountsFixtures
|
import Chesstrainer.AccountsFixtures
|
||||||
|
import Chesstrainer.EndgamesFixtures
|
||||||
alias Chesstrainer.Accounts.{User, UserToken}
|
alias Chesstrainer.Accounts.{User, UserToken}
|
||||||
|
alias Chesstrainer.Attempts
|
||||||
|
alias Chesstrainer.Attempts.Attempt
|
||||||
|
alias Chesstrainer.Accounts.Scope
|
||||||
|
|
||||||
describe "get_user_by_email/1" do
|
describe "get_user_by_email/1" do
|
||||||
test "does not return the user if the email does not exist" do
|
test "does not return the user if the email does not exist" do
|
||||||
@@ -432,6 +436,30 @@ defmodule Chesstrainer.AccountsTest do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "delete_user/1" do
|
||||||
|
test "deletes the user and their session tokens" do
|
||||||
|
user = user_fixture()
|
||||||
|
token = Accounts.generate_user_session_token(user)
|
||||||
|
|
||||||
|
assert {:ok, %User{}} = Accounts.delete_user(user)
|
||||||
|
|
||||||
|
refute Accounts.get_user_by_email(user.email)
|
||||||
|
refute Repo.get_by(UserToken, token: token)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "deletes the user's attempts" do
|
||||||
|
user = user_fixture()
|
||||||
|
endgame = endgame_fixture()
|
||||||
|
|
||||||
|
assert {:ok, attempt} =
|
||||||
|
Attempts.record_attempt(Scope.for_user(user), endgame, :solved)
|
||||||
|
|
||||||
|
assert {:ok, %User{}} = Accounts.delete_user(user)
|
||||||
|
|
||||||
|
refute Repo.get(Attempt, attempt.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "delete_user_session_token/1" do
|
describe "delete_user_session_token/1" do
|
||||||
test "deletes the token" do
|
test "deletes the token" do
|
||||||
user = user_fixture()
|
user = user_fixture()
|
||||||
|
|||||||
@@ -160,6 +160,44 @@ defmodule ChesstrainerWeb.UserLive.SettingsTest do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "delete account" do
|
||||||
|
setup %{conn: conn} do
|
||||||
|
user = user_fixture()
|
||||||
|
%{conn: log_in_user(conn, user), user: user}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "renders the permanent deletion warning", %{conn: conn} do
|
||||||
|
{:ok, _lv, html} = live(conn, ~p"/users/settings")
|
||||||
|
|
||||||
|
assert html =~ "Delete account"
|
||||||
|
assert html =~ "permanent and cannot be undone"
|
||||||
|
assert html =~ "Nothing can be recovered"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "deletes the user and redirects home", %{conn: conn, user: user} do
|
||||||
|
{:ok, lv, _html} = live(conn, ~p"/users/settings")
|
||||||
|
|
||||||
|
assert {:error, {:redirect, %{to: "/"}}} =
|
||||||
|
result = lv |> element("#delete_account_button") |> render_click()
|
||||||
|
|
||||||
|
refute Accounts.get_user_by_email(user.email)
|
||||||
|
|
||||||
|
{:ok, conn} = follow_redirect(result, conn, ~p"/")
|
||||||
|
assert conn.resp_body =~ "Your account has been deleted."
|
||||||
|
end
|
||||||
|
|
||||||
|
test "logs the deleted user out", %{conn: conn, user: user} do
|
||||||
|
{:ok, lv, _html} = live(conn, ~p"/users/settings")
|
||||||
|
|
||||||
|
lv |> element("#delete_account_button") |> render_click()
|
||||||
|
|
||||||
|
assert {:error, redirect} = live(conn, ~p"/users/settings")
|
||||||
|
assert {:redirect, %{to: path}} = redirect
|
||||||
|
assert path == ~p"/users/log-in"
|
||||||
|
refute Accounts.get_user_by_email(user.email)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "confirm email" do
|
describe "confirm email" do
|
||||||
setup %{conn: conn} do
|
setup %{conn: conn} do
|
||||||
user = user_fixture()
|
user = user_fixture()
|
||||||
|
|||||||
Reference in New Issue
Block a user