AshMultiAccount.Phoenix.LoadMultiAccount (AshMultiAccount v0.1.1)

Copy Markdown View Source

Plug that resolves multi-account assigns for controller-rendered pages.

Mirrors the logic of AshMultiAccount.Phoenix.LiveHook but for Plug.Conn: determines whether the session is in multi-account mode and sets :current_user and :primary_user assigns on the conn.

Usage

pipeline :browser do
  plug :fetch_session
  # ... other plugs ...
  plug AshMultiAccount.Phoenix.Plug
  plug AshMultiAccount.Phoenix.LoadMultiAccount, user_resource: MyApp.Accounts.User
end

Must run after :fetch_session and AshMultiAccount.Phoenix.Plug in the pipeline.

Assigns Set

  • :current_user — the user currently acting (may differ from primary in multi-account mode)
  • :primary_user — the primary account owner (nil when not in multi-account mode)

Error Handling

When the primary user is not found or not active, the plug clears the stale multi-account session keys and falls back to standard mode — resolving the current user from conn.assigns or the session without multi-account context. For unexpected errors (e.g., database failures), the plug assigns nil gracefully and lets the controller decide how to respond.