defmodule PhoenixKitWeb.Users.ResetPassword do @moduledoc """ LiveView for password reset functionality. Handles the password reset flow after a user clicks the reset link from their email. Validates the reset token and allows the user to set a new password. """ use PhoenixKitWeb, :live_view alias PhoenixKit.Users.Auth alias PhoenixKit.Utils.Routes def mount(params, _session, socket) do socket = assign_user_and_token(socket, params) form_source = case socket.assigns do %{user: user} -> Auth.change_user_password(user) _ -> %{} end {:ok, assign_form(socket, form_source), temporary_assigns: [form: nil]} end # Do not log in the user after reset password to avoid a # leaked token giving the user access to the account. def handle_event("reset_password", %{"user" => user_params}, socket) do case Auth.reset_user_password(socket.assigns.user, user_params) do {:ok, _} -> {:noreply, socket |> put_flash(:info, "Password reset successfully.") |> redirect(to: Routes.path("/users/log-in"))} {:error, changeset} -> {:noreply, assign_form(socket, Map.put(changeset, :action, :insert))} end end def handle_event("validate", %{"user" => user_params}, socket) do changeset = Auth.change_user_password(socket.assigns.user, user_params) {:noreply, assign_form(socket, Map.put(changeset, :action, :validate))} end defp assign_user_and_token(socket, %{"token" => token}) do if user = Auth.get_user_by_reset_password_token(token) do assign(socket, user: user, token: token) else socket |> put_flash(:error, "Reset password link is invalid or it has expired.") |> redirect(to: "/") end end defp assign_form(socket, %{} = source) do assign(socket, :form, to_form(source, as: "user")) end end