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

Provides `link_account/2` and `switch_to_account/2` controller actions for
multi-account linking and switching.

## Usage

    defmodule MyAppWeb.MultiAccountController do
      use MyAppWeb, :controller
      use AshMultiAccount.Phoenix.Controller,
        user_resource: MyApp.Accounts.User

      # Optionally override redirect paths:
      # def after_link_path(_conn), do: "/"
      # def after_switch_path(_conn), do: "/"
      # def sign_in_path(_conn, primary_user_id), do: "/sign-in?return_to=/link/p/#{primary_user_id}"
      # def sign_out_path(_conn), do: "/sign-out"
    end

## Options

- `user_resource` (required) — the Ash resource module for users.

## Actions

- `link_account(conn, %{"primary_user_id" => id})` — links the current user
  to an existing primary account, or sets up the multi-account session for the
  primary user themselves. Redirects to sign-in if no user is authenticated.
  For cross-user linking, GET renders an auto-submitting form (preserving
  REST semantics after the auth redirect), and POST creates the LinkedAccount
  record.

- `switch_to_account(conn, %{"user_id" => id})` — switches the current session
  to a different linked user. Rotates the session ID via
  `configure_session(renew: true)` to reduce session fixation risk.

---

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