defmodule LearningPhoenixWeb.UserLive.Confirmation do use LearningPhoenixWeb, :live_view alias LearningPhoenix.Accounts @impl true def render(assigns) do ~H"""
<.header>Welcome {@user.email}
<.form :if={!@user.confirmed_at} for={@form} id="confirmation_form" phx-mounted={JS.focus_first()} phx-submit="submit" action={~p"/users/log-in?_action=confirmed"} phx-trigger-action={@trigger_submit} > <.button name={@form[:remember_me].name} value="true" phx-disable-with="Confirming..." class="btn btn-primary w-full" > Confirm and stay logged in <.button phx-disable-with="Confirming..." class="btn btn-primary btn-soft w-full mt-2"> Confirm and log in only this time <.form :if={@user.confirmed_at} for={@form} id="login_form" phx-submit="submit" phx-mounted={JS.focus_first()} action={~p"/users/log-in"} phx-trigger-action={@trigger_submit} > <%= if @current_scope do %> <.button phx-disable-with="Logging in..." class="btn btn-primary w-full"> Log in <% else %> <.button name={@form[:remember_me].name} value="true" phx-disable-with="Logging in..." class="btn btn-primary w-full" > Keep me logged in on this device <.button phx-disable-with="Logging in..." class="btn btn-primary btn-soft w-full mt-2"> Log me in only this time <% end %>

Tip: If you prefer passwords, you can enable them in the user settings.

""" end @impl true def mount(%{"token" => token}, _session, socket) do if user = Accounts.get_user_by_magic_link_token(token) do form = to_form(%{"token" => token}, as: "user") {:ok, assign(socket, user: user, form: form, trigger_submit: false), temporary_assigns: [form: nil]} else {:ok, socket |> put_flash(:error, "Magic link is invalid or it has expired.") |> push_navigate(to: ~p"/users/log-in")} end end @impl true def handle_event("submit", %{"user" => params}, socket) do {:noreply, assign(socket, form: to_form(params, as: "user"), trigger_submit: true)} end end