# `AshMultiAccount.Phoenix.LoadMultiAccount`
[🔗](https://github.com/chriscox/ash_multi_account/blob/v0.1.1/lib/ash_multi_account/phoenix/load_multi_account.ex#L1)

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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
