defmodule CanaryWeb.LiveUser do
  import Phoenix.Component
  use CanaryWeb, :verified_routes

  def on_mount(:live_user_optional, _params, _session, socket) do
    if socket.assigns[:current_user] do
      {:cont, socket}
    else
      {:cont, assign(socket, :current_user, nil)}
    end
  end

  def on_mount(:live_user_required, _params, _session, socket) do
    cond do
      is_nil(socket.assigns[:current_user]) ->
        {:halt, Phoenix.LiveView.redirect(socket, to: ~p"/sign-in")}

      true ->
        {:cont, socket}
    end
  end

  def on_mount(:live_no_user, _params, _session, socket) do
    if socket.assigns[:current_user] do
      {:halt, Phoenix.LiveView.redirect(socket, to: ~p"/")}
    else
      {:cont, assign(socket, :current_user, nil)}
    end
  end
end
