diff --git a/lib/cannery/accounts.ex b/lib/cannery/accounts.ex index 0a8ee8a..b1294fb 100644 --- a/lib/cannery/accounts.ex +++ b/lib/cannery/accounts.ex @@ -244,6 +244,11 @@ defmodule Cannery.Accounts do end end + @spec delete_user!(User.t()) :: User.t() + def delete_user!(%User{} = user) do + user |> Repo.delete!() + end + ## Session @doc """ diff --git a/lib/cannery_web/controllers/user_settings_controller.ex b/lib/cannery_web/controllers/user_settings_controller.ex index 0fd6477..eb709d7 100644 --- a/lib/cannery_web/controllers/user_settings_controller.ex +++ b/lib/cannery_web/controllers/user_settings_controller.ex @@ -64,6 +64,20 @@ defmodule CanneryWeb.UserSettingsController do end end + def delete(conn, %{"id" => user_id}) do + if user_id == conn.assigns.current_user.id do + Accounts.delete_user!(conn.assigns.current_user) + + conn + |> put_flash(:error, "Your account has been deleted") + |> redirect(to: Routes.home_path(conn, :index)) + else + conn + |> put_flash(:error, "Unable to delete user") + |> redirect(to: Routes.user_settings_path(conn, :edit)) + end + end + defp assign_email_and_password_changesets(conn, _opts) do user = conn.assigns.current_user diff --git a/lib/cannery_web/router.ex b/lib/cannery_web/router.ex index 4d78e86..a14aa2b 100644 --- a/lib/cannery_web/router.ex +++ b/lib/cannery_web/router.ex @@ -12,7 +12,7 @@ defmodule CanneryWeb.Router do plug :put_secure_browser_headers plug :fetch_current_user end - + pipeline :require_admin do plug :require_role, role: :admin end @@ -26,7 +26,7 @@ defmodule CanneryWeb.Router do live "/", HomeLive, :index end - + ## Authentication routes scope "/", CanneryWeb do @@ -47,6 +47,7 @@ defmodule CanneryWeb.Router do get "/users/settings", UserSettingsController, :edit put "/users/settings", UserSettingsController, :update + delete "/users/settings/:id", UserSettingsController, :delete get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email live "/tags", TagLive.Index, :index @@ -80,7 +81,7 @@ defmodule CanneryWeb.Router do scope "/", CanneryWeb do pipe_through [:browser, :require_authenticated_user, :require_admin] - + live_dashboard "/dashboard", metrics: CanneryWeb.Telemetry, ecto_repos: [Cannery.Repo] live "/invites", InviteLive.Index, :index diff --git a/lib/cannery_web/templates/user_settings/edit.html.eex b/lib/cannery_web/templates/user_settings/edit.html.eex index c01db3a..c17956a 100644 --- a/lib/cannery_web/templates/user_settings/edit.html.eex +++ b/lib/cannery_web/templates/user_settings/edit.html.eex @@ -1,4 +1,4 @@ -