daily-tracker

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

confirmation.ex (3038B)


      1 defmodule DailyTrackerWeb.UserLive.Confirmation do
      2   use DailyTrackerWeb, :live_view
      3 
      4   alias DailyTracker.Accounts
      5 
      6   @impl true
      7   def render(assigns) do
      8     ~H"""
      9     <Layouts.app flash={@flash} current_scope={@current_scope}>
     10       <div class="mx-auto max-w-sm">
     11         <div class="text-center">
     12           <.header>Welcome {@user.email}</.header>
     13         </div>
     14 
     15         <.form
     16           :if={!@user.confirmed_at}
     17           for={@form}
     18           id="confirmation_form"
     19           phx-mounted={JS.focus_first()}
     20           phx-submit="submit"
     21           action={~p"/users/log-in?_action=confirmed"}
     22           phx-trigger-action={@trigger_submit}
     23         >
     24           <input type="hidden" name={@form[:token].name} value={@form[:token].value} />
     25           <.button
     26             name={@form[:remember_me].name}
     27             value="true"
     28             phx-disable-with="Confirming..."
     29             class="btn btn-primary w-full"
     30           >
     31             Confirm and stay logged in
     32           </.button>
     33           <.button phx-disable-with="Confirming..." class="btn btn-primary btn-soft w-full mt-2">
     34             Confirm and log in only this time
     35           </.button>
     36         </.form>
     37 
     38         <.form
     39           :if={@user.confirmed_at}
     40           for={@form}
     41           id="login_form"
     42           phx-submit="submit"
     43           phx-mounted={JS.focus_first()}
     44           action={~p"/users/log-in"}
     45           phx-trigger-action={@trigger_submit}
     46         >
     47           <input type="hidden" name={@form[:token].name} value={@form[:token].value} />
     48           <%= if @current_scope do %>
     49             <.button phx-disable-with="Logging in..." class="btn btn-primary w-full">
     50               Log in
     51             </.button>
     52           <% else %>
     53             <.button
     54               name={@form[:remember_me].name}
     55               value="true"
     56               phx-disable-with="Logging in..."
     57               class="btn btn-primary w-full"
     58             >
     59               Keep me logged in on this device
     60             </.button>
     61             <.button phx-disable-with="Logging in..." class="btn btn-primary btn-soft w-full mt-2">
     62               Log me in only this time
     63             </.button>
     64           <% end %>
     65         </.form>
     66 
     67         <p :if={!@user.confirmed_at} class="alert alert-outline mt-8">
     68           Tip: If you prefer passwords, you can enable them in the user settings.
     69         </p>
     70       </div>
     71     </Layouts.app>
     72     """
     73   end
     74 
     75   @impl true
     76   def mount(%{"token" => token}, _session, socket) do
     77     if user = Accounts.get_user_by_magic_link_token(token) do
     78       form = to_form(%{"token" => token}, as: "user")
     79 
     80       {:ok, assign(socket, user: user, form: form, trigger_submit: false),
     81        temporary_assigns: [form: nil]}
     82     else
     83       {:ok,
     84        socket
     85        |> put_flash(:error, "Magic link is invalid or it has expired.")
     86        |> push_navigate(to: ~p"/users/log-in")}
     87     end
     88   end
     89 
     90   @impl true
     91   def handle_event("submit", %{"user" => params}, socket) do
     92     {:noreply, assign(socket, form: to_form(params, as: "user"), trigger_submit: true)}
     93   end
     94 end