add delete user function

This commit is contained in:
shibao 2021-09-10 21:37:17 -04:00 committed by oliviasculley
parent d424a6631a
commit 8e4e3872f5
4 changed files with 32 additions and 8 deletions

View File

@ -244,6 +244,11 @@ defmodule Cannery.Accounts do
end end
end end
@spec delete_user!(User.t()) :: User.t()
def delete_user!(%User{} = user) do
user |> Repo.delete!()
end
## Session ## Session
@doc """ @doc """

View File

@ -64,6 +64,20 @@ defmodule CanneryWeb.UserSettingsController do
end end
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 defp assign_email_and_password_changesets(conn, _opts) do
user = conn.assigns.current_user user = conn.assigns.current_user

View File

@ -12,7 +12,7 @@ defmodule CanneryWeb.Router do
plug :put_secure_browser_headers plug :put_secure_browser_headers
plug :fetch_current_user plug :fetch_current_user
end end
pipeline :require_admin do pipeline :require_admin do
plug :require_role, role: :admin plug :require_role, role: :admin
end end
@ -26,7 +26,7 @@ defmodule CanneryWeb.Router do
live "/", HomeLive, :index live "/", HomeLive, :index
end end
## Authentication routes ## Authentication routes
scope "/", CanneryWeb do scope "/", CanneryWeb do
@ -47,6 +47,7 @@ defmodule CanneryWeb.Router do
get "/users/settings", UserSettingsController, :edit get "/users/settings", UserSettingsController, :edit
put "/users/settings", UserSettingsController, :update put "/users/settings", UserSettingsController, :update
delete "/users/settings/:id", UserSettingsController, :delete
get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email
live "/tags", TagLive.Index, :index live "/tags", TagLive.Index, :index
@ -80,7 +81,7 @@ defmodule CanneryWeb.Router do
scope "/", CanneryWeb do scope "/", CanneryWeb do
pipe_through [:browser, :require_authenticated_user, :require_admin] pipe_through [:browser, :require_authenticated_user, :require_admin]
live_dashboard "/dashboard", metrics: CanneryWeb.Telemetry, ecto_repos: [Cannery.Repo] live_dashboard "/dashboard", metrics: CanneryWeb.Telemetry, ecto_repos: [Cannery.Repo]
live "/invites", InviteLive.Index, :index live "/invites", InviteLive.Index, :index

View File

@ -1,4 +1,4 @@
<div class="flex flex-col justify-center items-center space-y-4"> <div class="mb-8 flex flex-col justify-center items-center space-y-8">
<h1 class="title text-primary-500 text-xl"> <h1 class="title text-primary-500 text-xl">
Settings Settings
</h1> </h1>
@ -22,7 +22,7 @@
<%= email_input f, :email, required: true, class: "input input-primary col-span-2" %> <%= email_input f, :email, required: true, class: "input input-primary col-span-2" %>
</div> </div>
<%= error_tag f, :email %> <%= error_tag f, :email %>
<div class="grid grid-cols-3 justify-center items-center text-center space-x-4"> <div class="grid grid-cols-3 justify-center items-center text-center space-x-4">
<%= label f, :current_password, for: "current_password_for_email", class: "title text-lg text-primary-500" %> <%= label f, :current_password, for: "current_password_for_email", class: "title text-lg text-primary-500" %>
<%= password_input f, :current_password, <%= password_input f, :current_password,
@ -58,7 +58,7 @@
class: "input input-primary col-span-2" %> class: "input input-primary col-span-2" %>
</div> </div>
<%= error_tag f, :password %> <%= error_tag f, :password %>
<div class="grid grid-cols-3 justify-center items-center text-center space-x-4"> <div class="grid grid-cols-3 justify-center items-center text-center space-x-4">
<%= label f, :password_confirmation, "Confirm new password", <%= label f, :password_confirmation, "Confirm new password",
class: "title text-lg text-primary-500" %> class: "title text-lg text-primary-500" %>
@ -67,7 +67,7 @@
class: "input input-primary col-span-2" %> class: "input input-primary col-span-2" %>
</div> </div>
<%= error_tag f, :password_confirmation %> <%= error_tag f, :password_confirmation %>
<div class="grid grid-cols-3 justify-center items-center text-center space-x-4"> <div class="grid grid-cols-3 justify-center items-center text-center space-x-4">
<%= label f, :current_password, <%= label f, :current_password,
for: "current_password_for_password", for: "current_password_for_password",
@ -82,4 +82,8 @@
<%= submit "Change password", class: "btn btn-primary" %> <%= submit "Change password", class: "btn btn-primary" %>
<% end %> <% end %>
</div>
<%= link "Delete User", to: Routes.user_settings_path(@conn, :delete, @current_user),
method: :delete, class: "btn btn-alert",
data: [confirm: "Are you sure you want to delete your account?"] %>
</div>