defmodule LearningPhoenixWeb.UserLive.Login do use LearningPhoenixWeb, :live_view alias LearningPhoenix.Accounts @impl true def render(assigns) do ~H"""
<.header>

Log in

<:subtitle> <%= if @current_scope do %> You need to reauthenticate to perform sensitive actions on your account. <% else %> Don't have an account? <.link navigate={~p"/users/register"} class="font-semibold text-brand hover:underline" phx-no-format >Sign up for an account now. <% end %>
<.icon name="hero-information-circle" class="size-6 shrink-0" />

You are running the local mail adapter.

To see sent emails, visit <.link href="/dev/mailbox" class="underline">the mailbox page.

<.form :let={f} for={@form} id="login_form_magic" action={~p"/users/log-in"} phx-submit="submit_magic" > <.input readonly={!!@current_scope} field={f[:email]} type="email" label="Email" autocomplete="username" required phx-mounted={JS.focus()} /> <.button class="btn btn-primary w-full"> Log in with email
or
<.form :let={f} for={@form} id="login_form_password" action={~p"/users/log-in"} phx-submit="submit_password" phx-trigger-action={@trigger_submit} > <.input readonly={!!@current_scope} field={f[:email]} type="email" label="Email" autocomplete="username" required /> <.input field={@form[:password]} type="password" label="Password" autocomplete="current-password" /> <.button class="btn btn-primary w-full" name={@form[:remember_me].name} value="true"> Log in and stay logged in <.button class="btn btn-primary btn-soft w-full mt-2"> Log in only this time
""" end @impl true def mount(_params, _session, socket) do email = Phoenix.Flash.get(socket.assigns.flash, :email) || get_in(socket.assigns, [:current_scope, Access.key(:user), Access.key(:email)]) form = to_form(%{"email" => email}, as: "user") {:ok, assign(socket, form: form, trigger_submit: false)} end @impl true def handle_event("submit_password", _params, socket) do {:noreply, assign(socket, :trigger_submit, true)} end def handle_event("submit_magic", %{"user" => %{"email" => email}}, socket) do if user = Accounts.get_user_by_email(email) do Accounts.deliver_login_instructions( user, &url(~p"/users/log-in/#{&1}") ) end info = "If your email is in our system, you will receive instructions for logging in shortly." {:noreply, socket |> put_flash(:info, info) |> push_navigate(to: ~p"/users/log-in")} end defp local_mail_adapter? do Application.get_env(:learning_phoenix, LearningPhoenix.Mailer)[:adapter] == Swoosh.Adapters.Local end end