Add change password form

This commit is contained in:
Alex
2022-11-30 20:55:38 +01:00
parent f9e1acd5b2
commit e8eaef27c5
9 changed files with 220 additions and 132 deletions

View File

@@ -17,11 +17,11 @@ defmodule ClaperWeb.UserRegistrationController do
def create(conn, %{"user" => user_params}) do
case Accounts.register_user(user_params) do
{:ok, user} ->
{:ok, _} =
Accounts.deliver_user_confirmation_instructions(
user,
&Routes.user_confirmation_url(conn, :update, &1)
)
#{:ok, _} =
# Accounts.deliver_user_confirmation_instructions(
# user,
# &Routes.user_confirmation_url(conn, :update, &1)
# )
conn
|> put_flash(:info, "User created successfully.")

View File

@@ -1,47 +0,0 @@
defmodule ClaperWeb.UserSettingsLive.FormComponent do
use ClaperWeb, :live_component
alias Claper.Accounts
@impl true
def update(assigns, socket) do
email_changeset = Accounts.User.email_changeset(%Accounts.User{}, %{})
{:ok,
socket
|> assign(:email_changeset, email_changeset)
|> assign(assigns)}
end
@impl true
def handle_event("save", %{"action" => "update_email"} = params, socket) do
%{"user" => user_params} = params
user = socket.assigns.current_user
case Accounts.apply_user_email(user, user_params) do
{:ok, applied_user} ->
Accounts.deliver_update_email_instructions(
applied_user,
user.email,
&Routes.user_settings_url(socket, :confirm_email, &1)
)
{:noreply,
socket
|> put_flash(
:info,
gettext("A link to confirm your email change has been sent to the new address.")
)
|> push_redirect(to: socket.assigns.return_to)}
{:error, changeset} ->
{:noreply, assign(socket, :email_changeset, changeset)}
end
end
@impl true
def handle_event("validate", _params, socket) do
{:noreply, socket}
end
end

View File

@@ -1,13 +0,0 @@
<div>
<%= if @action == :edit_email do %>
<.form let={f} for={@email_changeset} phx-target={@myself} phx-submit="save" id="update_email" class="mt-5 md:flex md:items-end">
<%= hidden_input f, :action, name: "action", value: "update_email" %>
<ClaperWeb.Component.Input.email form={f} key={:email} name={gettext "Email"} required="true" />
<%= submit gettext("Save"), phx_disable_with: "Saving...", class: "mt-2 w-full h-14 inline-flex transition-all items-center justify-center px-4 py-2 shadow-sm font-medium rounded-md text-white bg-black hover:bg-primary-500 md:mt-0 md:ml-3 md:w-auto md:text-sm" %>
</.form>
<% end %>
</div>

View File

@@ -12,8 +12,9 @@ defmodule ClaperWeb.UserSettingsLive.Show do
end
email_changeset = Accounts.User.email_changeset(%Accounts.User{}, %{})
password_changeset = Accounts.User.password_changeset(%Accounts.User{}, %{})
{:ok, socket |> assign(:email_changeset, email_changeset)}
{:ok, socket |> assign(:email_changeset, email_changeset) |> assign(:password_changeset, password_changeset)}
end
@impl true
@@ -30,6 +31,15 @@ defmodule ClaperWeb.UserSettingsLive.Show do
)
end
defp apply_action(socket, :edit_password, _params) do
socket
|> assign(:page_title, gettext("Update your password"))
|> assign(
:page_description,
gettext("Change the password used to access your account.")
)
end
defp apply_action(socket, :show, _params) do
socket
|> assign(:page_title, gettext("Settings"))
@@ -62,6 +72,29 @@ defmodule ClaperWeb.UserSettingsLive.Show do
end
end
@impl true
def handle_event("save", %{"action" => "update_password"} = params, socket) do
%{"user" => user_params} = params
%{"current_password" => password} = user_params
user = socket.assigns.current_user
case Accounts.update_user_password(user, password, user_params) do
{:ok, applied_user} ->
{:noreply,
socket
|> put_flash(
:info,
gettext("Your password has been updated.")
)
|> push_redirect(to: Routes.user_settings_show_path(socket, :show))}
{:error, changeset} ->
{:noreply, assign(socket, :password_changeset, changeset)}
end
end
@impl true
def handle_event("validate", _params, socket) do
{:noreply, socket}

View File

@@ -24,7 +24,30 @@
<%= hidden_input f, :action, name: "action", value: "update_email" %>
<ClaperWeb.Component.Input.email form={f} key={:email} name={gettext "Email"} required="true" />
<%= submit gettext("Save"), phx_disable_with: "Saving...", class: "mt-2 w-full h-14 inline-flex transition-all items-center justify-center px-4 py-2 shadow-sm font-medium rounded-md text-white bg-black hover:bg-primary-500 md:mt-0 md:ml-3 md:w-auto md:text-sm" %>
</.form>
</div>
</.live_component>
<% end %>
<%= if @live_action in [:edit_password] do %>
<.live_component module={ClaperWeb.ModalComponent}
class="hidden"
id="modal-wrapper"
title={@page_title}
description={@page_description}
return_to={Routes.user_settings_show_path(@socket, :show)}>
<div>
<.form let={f} for={@password_changeset} phx-submit="save" id="update_password" class="mt-5 md:flex md:items-end gap-x-2">
<%= hidden_input f, :action, name: "action", value: "update_password" %>
<ClaperWeb.Component.Input.password form={f} key={:current_password} name={gettext "Current password"} required="true" />
<ClaperWeb.Component.Input.password form={f} key={:password} name={gettext "New password"} required="true" />
<%= submit gettext("Save"), phx_disable_with: "Saving...", class: "mt-2 w-full h-14 inline-flex transition-all items-center justify-center px-4 py-2 shadow-sm font-medium rounded-md text-white bg-black hover:bg-primary-500 md:mt-0 md:ml-3 md:w-auto md:text-sm" %>
</.form>
</div>
@@ -53,9 +76,19 @@
<%= live_patch gettext("Change"), to: Routes.user_settings_show_path(@socket, :edit_email), class: "rounded-md font-medium text-purple-600 hover:text-purple-500" %>
</span>
</dd>
<dt class="text-sm font-medium text-gray-500">
<%= gettext "Password" %>
</dt>
<dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
<span class="flex-grow">********</span>
<span class="ml-4 flex-shrink-0">
<%= live_patch gettext("Change"), to: Routes.user_settings_show_path(@socket, :edit_password), class: "rounded-md font-medium text-purple-600 hover:text-purple-500" %>
</span>
</dd>
</div>
</dl>
</div>
</div>
</div>
</div>
</div>