api-v2/deps/phoenix/priv/templates/phx.gen.auth/confirmation_live.ex
2025-04-16 10:03:13 -03:00

59 lines
2.3 KiB
Elixir

defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>ConfirmationLive do
use <%= inspect context.web_module %>, :live_view
alias <%= inspect context.module %>
def render(%{live_action: :edit} = assigns) do
~H"""
<div class="mx-auto max-w-sm">
<.header class="text-center">Confirm Account</.header>
<.simple_form for={@form} id="confirmation_form" phx-submit="confirm_account">
<input type="hidden" name={@form[:token].name} value={@form[:token].value} />
<:actions>
<.button phx-disable-with="Confirming..." class="w-full">Confirm my account</.button>
</:actions>
</.simple_form>
<p class="text-center mt-4">
<.link href={~p"<%= schema.route_prefix %>/register"}>Register</.link>
| <.link href={~p"<%= schema.route_prefix %>/log_in"}>Log in</.link>
</p>
</div>
"""
end
def mount(%{"token" => token}, _session, socket) do
form = to_form(%{"token" => token}, as: "<%= schema.singular %>")
{:ok, assign(socket, form: form), temporary_assigns: [form: nil]}
end
# Do not log in the <%= schema.singular %> after confirmation to avoid a
# leaked token giving the <%= schema.singular %> access to the account.
def handle_event("confirm_account", %{"<%= schema.singular %>" => %{"token" => token}}, socket) do
case <%= inspect context.alias %>.confirm_<%= schema.singular %>(token) do
{:ok, _} ->
{:noreply,
socket
|> put_flash(:info, "<%= inspect schema.alias %> confirmed successfully.")
|> redirect(to: ~p"/")}
:error ->
# If there is a current <%= schema.singular %> and the account was already confirmed,
# then odds are that the confirmation link was already visited, either
# by some automation or by the <%= schema.singular %> themselves, so we redirect without
# a warning message.
case socket.assigns do
%{current_<%= schema.singular %>: %{confirmed_at: confirmed_at}} when not is_nil(confirmed_at) ->
{:noreply, redirect(socket, to: ~p"/")}
%{} ->
{:noreply,
socket
|> put_flash(:error, "<%= inspect schema.alias %> confirmation link is invalid or it has expired.")
|> redirect(to: ~p"/")}
end
end
end
end