Current section
Files
Jump to
Current section
Files
priv/templates/auth_live_reset_password.eex
defmodule <%= inspect @module %> do
@moduledoc """
Password-reset LiveView generated by Caravela from <%= inspect @domain_module %>.
Two-phase: `/auth/reset-password` asks for an email and triggers the
reset email; `/auth/reset-password/:token` collects the new password
and commits it.
"""
use <%= @web_module_alias %>, :live_view
alias <%= inspect @auth_module %>, as: Auth
@impl true
def mount(params, _session, socket) do
token = Map.get(params, "token")
mode = if token, do: "confirm", else: "request"
{:ok,
socket
|> assign(:mode, mode)
|> assign(:token, token)
|> assign(:errors, %{})
|> assign(:loading, false)
|> assign(:sent, false)
|> assign(:flash_message, nil)}
end
@impl true
def handle_event("request_reset", %{"email" => email}, socket) do
_ = Auth.request_password_reset(email, request_context(socket))
# Always show the same "sent" state to avoid leaking existing emails.
{:noreply, assign(socket, loading: false, sent: true)}
end
def handle_event(
"reset_password",
%{"password" => p, "password_confirmation" => p, "token" => token},
socket
) do
case Auth.reset_password(token, p) do
{:ok, _user} ->
{:noreply,
socket
|> put_flash(:info, "Password updated — please sign in.")
|> push_navigate(to: "<%= @login_path %>")}
{:error, _} ->
{:noreply, assign(socket, errors: %{token: ["This reset link is invalid or has expired."]})}
end
end
def handle_event("reset_password", _params, socket) do
{:noreply, assign(socket, errors: %{password_confirmation: ["does not match"]})}
end
@impl true
def render(assigns) do
~H"""
<LiveSvelte.svelte
name="<%= @component_ref %>"
props={%{
mode: @mode,
token: @token,
errors: @errors,
loading: @loading,
sent: @sent,
flash_message: @flash_message
}}
socket={@socket}
/>
"""
end
defp request_context(socket) do
%{
current_user: nil<%= if @multi_tenant do %>,
tenant_id: socket.assigns[:tenant_id]<% end %>
}
end
# --- CUSTOM :auth_live_reset_password ---
# --- END :auth_live_reset_password ---
<%= @custom_marker %>