|
| 1 | +defmodule RadiatorWeb.UserResetPasswordLive do |
| 2 | + use RadiatorWeb, :live_view |
| 3 | + |
| 4 | + alias Radiator.Accounts |
| 5 | + |
| 6 | + def render(assigns) do |
| 7 | + ~H""" |
| 8 | + <div class="mx-auto max-w-sm"> |
| 9 | + <.header class="text-center">Reset Password</.header> |
| 10 | +
|
| 11 | + <.simple_form |
| 12 | + for={@form} |
| 13 | + id="reset_password_form" |
| 14 | + phx-submit="reset_password" |
| 15 | + phx-change="validate" |
| 16 | + > |
| 17 | + <.error :if={@form.errors != []}> |
| 18 | + Oops, something went wrong! Please check the errors below. |
| 19 | + </.error> |
| 20 | +
|
| 21 | + <.input field={@form[:password]} type="password" label="New password" required /> |
| 22 | + <.input |
| 23 | + field={@form[:password_confirmation]} |
| 24 | + type="password" |
| 25 | + label="Confirm new password" |
| 26 | + required |
| 27 | + /> |
| 28 | + <:actions> |
| 29 | + <.button phx-disable-with="Resetting..." class="w-full">Reset Password</.button> |
| 30 | + </:actions> |
| 31 | + </.simple_form> |
| 32 | +
|
| 33 | + <p class="text-center text-sm mt-4"> |
| 34 | + <.link href={~p"/users/register"}>Register</.link> |
| 35 | + | <.link href={~p"/users/log-in"}>Log in</.link> |
| 36 | + </p> |
| 37 | + </div> |
| 38 | + """ |
| 39 | + end |
| 40 | + |
| 41 | + def mount(params, _session, socket) do |
| 42 | + socket = assign_user_and_token(socket, params) |
| 43 | + |
| 44 | + form_source = |
| 45 | + case socket.assigns do |
| 46 | + %{user: user} -> |
| 47 | + Accounts.change_user_password(user) |
| 48 | + |
| 49 | + _ -> |
| 50 | + %{} |
| 51 | + end |
| 52 | + |
| 53 | + {:ok, assign_form(socket, form_source), temporary_assigns: [form: nil]} |
| 54 | + end |
| 55 | + |
| 56 | + # Do not log in the user after reset password to avoid a |
| 57 | + # leaked token giving the user access to the account. |
| 58 | + def handle_event("reset_password", %{"user" => user_params}, socket) do |
| 59 | + case Accounts.reset_user_password(socket.assigns.user, user_params) do |
| 60 | + {:ok, _} -> |
| 61 | + {:noreply, |
| 62 | + socket |
| 63 | + |> put_flash(:info, "Password reset successfully.") |
| 64 | + |> redirect(to: ~p"/users/log-in")} |
| 65 | + |
| 66 | + {:error, changeset} -> |
| 67 | + {:noreply, assign_form(socket, Map.put(changeset, :action, :insert))} |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def handle_event("validate", %{"user" => user_params}, socket) do |
| 72 | + changeset = Accounts.change_user_password(socket.assigns.user, user_params) |
| 73 | + {:noreply, assign_form(socket, Map.put(changeset, :action, :validate))} |
| 74 | + end |
| 75 | + |
| 76 | + defp assign_user_and_token(socket, %{"token" => token}) do |
| 77 | + if user = Accounts.get_user_by_reset_password_token(token) do |
| 78 | + assign(socket, user: user, token: token) |
| 79 | + else |
| 80 | + socket |
| 81 | + |> put_flash(:error, "Reset password link is invalid or it has expired.") |
| 82 | + |> redirect(to: ~p"/") |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + defp assign_form(socket, %{} = source) do |
| 87 | + assign(socket, :form, to_form(source, as: "user")) |
| 88 | + end |
| 89 | + |
| 90 | + @doc """ |
| 91 | + Renders a simple form. |
| 92 | +
|
| 93 | + ## Examples |
| 94 | +
|
| 95 | + <.simple_form for={@form} phx-change="validate" phx-submit="save"> |
| 96 | + <.input field={@form[:email]} label="Email"/> |
| 97 | + <.input field={@form[:username]} label="Username" /> |
| 98 | + <:actions> |
| 99 | + <.button>Save</.button> |
| 100 | + </:actions> |
| 101 | + </.simple_form> |
| 102 | + """ |
| 103 | + attr :for, :any, required: true, doc: "the data structure for the form" |
| 104 | + attr :as, :any, default: nil, doc: "the server side parameter to collect all input under" |
| 105 | + |
| 106 | + attr :rest, :global, |
| 107 | + include: ~w(autocomplete name rel action enctype method novalidate target multipart), |
| 108 | + doc: "the arbitrary HTML attributes to apply to the form tag" |
| 109 | + |
| 110 | + slot :inner_block, required: true |
| 111 | + slot :actions, doc: "the slot for form actions, such as a submit button" |
| 112 | + |
| 113 | + def simple_form(assigns) do |
| 114 | + ~H""" |
| 115 | + <.form :let={f} for={@for} as={@as} {@rest}> |
| 116 | + <div class="mt-10 space-y-8 bg-white"> |
| 117 | + {render_slot(@inner_block, f)} |
| 118 | + <div :for={action <- @actions} class="mt-2 flex items-center justify-between gap-6"> |
| 119 | + {render_slot(action, f)} |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </.form> |
| 123 | + """ |
| 124 | + end |
| 125 | + |
| 126 | + @doc """ |
| 127 | + Generates a generic error message. |
| 128 | + """ |
| 129 | + slot :inner_block, required: true |
| 130 | + |
| 131 | + def error(assigns) do |
| 132 | + ~H""" |
| 133 | + <p class="mt-3 flex gap-3 text-sm leading-6 text-rose-600"> |
| 134 | + <.icon name="hero-exclamation-circle-mini" class="mt-0.5 h-5 w-5 flex-none" /> |
| 135 | + {render_slot(@inner_block)} |
| 136 | + </p> |
| 137 | + """ |
| 138 | + end |
| 139 | +end |
0 commit comments