# `AshPhoenix.SubdomainPlug`
[🔗](https://github.com/ash-project/ash_phoenix/blob/v2.3.21/lib/ash_phoenix/plug/subdomain_plug.ex#L5)

This is a basic plug that loads the current tenant assign from a given
value set on subdomain.

This was copied from `Triplex.SubdomainPlug`, here:
  https://github.com/ateliware/triplex/blob/master/lib/triplex/plugs/subdomain_plug.ex

Options:

* `:endpoint` (`t:atom/0`) - Required. The endpoint that the plug is in, used for deterining the host

* `:assign` (`t:atom/0`) - The key to use when assigning the current tenant The default value is `:current_tenant`.

* `:handle_subdomain` - An mfa to call with the conn and a subdomain value. Can be used to do something like fetch the current user given the tenant. Must return the new conn.

To plug it on your router, you can use:
    plug AshPhoenix.SubdomainPlug,
      endpoint: MyApp.Endpoint

An additional helper here can be used for determining the host in your liveview, and/or using
the host that was already assigned to the conn.

For example:

    def handle_params(params, uri, socket) do
      socket =
        assign_new(socket, :current_tenant, fn ->
          AshPhoenix.SubdomainPlug.live_tenant(socket, uri)
        end)

      socket =
        assign_new(socket, :current_organization, fn ->
          if socket.assigns[:current_tenant] do
            MyApp.Accounts.Ash.get!(MyApp.Accounts.Organization,
              subdomain: socket.assigns[:current_tenant]
            )
          end
        end)

      {:noreply, socket}
    end

# `live_tenant`

---

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