team-alembic / ash_authentication

The Ash Authentication framework
MIT License
96 stars 55 forks source link

Proposal: Allow opts to `AshAuthentication.Plug.Helpers.retrieve_from_session/2` #846

Closed nallwhy closed 2 days ago

nallwhy commented 4 days ago

Currently, it is not possible to configure options, such as load options, when retrieving a user from a token using AshAuthentication.Plug.Helpers.retrieve_from_session/2.

I propose modifying the AshAuthentication.Plug.Helpers.retrieve_from_session/2 function to accept opts as an additional argument. This change would allow greater flexibility when loading users from tokens and make the function more versatile for different use cases.

Here is an example of how the function could be adjusted to accommodate opts.

defmodule AshAuthentication.Plug.Helpers do
  ...
  def retrieve_from_session(conn, otp_app, opts \\ []) do
    ...
    {:ok, user} <-
      AshAuthentication.subject_to_user(subject, resource, [
                     tenant: Ash.PlugHelpers.get_tenant(conn),
                     context: Ash.PlugHelpers.get_context(conn) || %{}
                   ] |> Keyword.merge(opts))
    ...
  end
end

defmodule AshAuthentication.Plug.Macros do
  ...
  defmacro define_load_from_session(otp_app) do
    quote do
      def load_from_session(conn, opts),
        do: Helpers.retrieve_from_session(conn, unquote(otp_app), opts)
      end
    end
  end
end
zachdaniel commented 3 days ago

Sounds good to me 👍