defmodule PlausibleWeb.Live.ResetPasswordForm do @moduledoc """ LiveView for password reset form. """ use Phoenix.LiveView use Phoenix.HTML import PlausibleWeb.Live.Components.Form alias Plausible.Auth alias Plausible.Repo def mount(_params, %{"email" => email}, socket) do socket = assign_new(socket, :user, fn -> Repo.get_by!(Auth.User, email: email) end) changeset = Auth.User.settings_changeset(socket.assigns.user) {:ok, assign(socket, form: to_form(changeset), password_strength: Auth.User.password_strength(changeset), trigger_submit: false )} end def render(assigns) do ~H""" <.form :let={f} for={@form} method="post" phx-change="validate" phx-submit="set" phx-trigger-action={@trigger_submit} class="bg-white dark:bg-gray-800 max-w-md w-full mx-auto shadow-md rounded px-8 py-6 mt-8" >
Don't have an account? <%= link("Register", to: "/register", class: "underline text-gray-800 dark:text-gray-200" ) %> instead.
""" end def handle_event("validate", %{"user" => %{"password" => password}}, socket) do changeset = socket.assigns.user |> Auth.User.set_password(password) |> Map.put(:action, :validate) password_strength = Auth.User.password_strength(changeset) {:noreply, assign(socket, form: to_form(changeset), password_strength: password_strength)} end def handle_event("set", %{"user" => %{"password" => password}}, socket) do user = Auth.User.set_password(socket.assigns.user, password) case Repo.update(user) do {:ok, _user} -> {:noreply, assign(socket, trigger_submit: true)} {:error, changeset} -> {:noreply, assign(socket, form: to_form(Map.put(changeset, :action, :validate)) )} end end end