defmodule AppWeb.LiveHelpers do
  @moduledoc """
  Commonly functions for LiveViews.
  """
  import Phoenix.LiveView

  alias Legendary.Auth.User
  alias Pow.Store.CredentialsCache
  alias AppWeb.Pow.Routes

  def assign_defaults(socket, session) do
    assign_new(socket, :current_user, fn -> get_user(socket, session) end)
  end

  def require_auth(socket) do
    if socket.assigns.current_user do
      socket
    else
      redirect(socket, to: Routes.after_sign_out_path(%Plug.Conn{}))
    end
  end

  defp get_user(socket, session, config \\ [otp_app: :core])

  defp get_user(socket, %{"core_auth" => signed_token}, config) do
    {otp_app, _config} = Keyword.pop(config, :otp_app, :core)
    {store, store_config} = Pow.Plug.Base.store(Application.get_env(otp_app, :pow))

    conn = struct!(Plug.Conn, secret_key_base: socket.endpoint.config(:secret_key_base))
    salt = Atom.to_string(Pow.Plug.Session)

    with {:ok, token} <- Pow.Plug.verify_token(conn, salt, signed_token, config),
        {user, _metadata} <- store.get(store_config, token) do
      user
    else
      _any -> nil
    end
  end

  defp get_user(_, _, _), do: nil
end
