# `AshPhoenix.LiveView.SubdomainHook`
[🔗](https://github.com/ash-project/ash_phoenix/blob/v2.3.21/lib/ash_phoenix/live_view/subdomain_hook.ex#L5)

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

Options:

* `: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 socket and a subdomain value. Can be used to do something like fetch the current user given the tenant.
          Must return either `{:cont, socket}`, `{:cont, socket, opts} or `{:halt, socket}`.

To use the hook, you can do one of the following:

```elixir
live_session :foo, on_mount: [
  AshPhoenix.LiveView.SubdomainHook,
]
```
This will assign the tenant's subdomain value to `:current_tenant` key by default.

If you want to specify the assign key

```elixir
live_session :foo, on_mount: [
  {AshPhoenix.LiveView.SubdomainHook, [assign: :different_assign_key}]
]
```

You can also provide `handle_subdomain` module, function, arguments tuple
that will be run after the tenant is assigned.

```elixir
live_session :foo, on_mount: [
  {AshPhoenix.LiveView.SubdomainHook, [handle_subdomain: {FooApp.SubdomainHandler, :handle_subdomain, [:bar]}]
]
```

This can be any module, function, and list of arguments as it uses Elixir's [apply/3](https://hexdocs.pm/elixir/1.18.3/Kernel.html#apply/3).

The socket and tenant will be the first two arguments.

The function return must match Phoenix LiveView's [on_mount/1](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#on_mount/1)

```elixir
defmodule FooApp.SubdomainHandler do
  def handle_subdomain(socket, tenant, :bar) do
    # your logic here
    {:cont, socket}
  end
end
```

# `on_mount`

---

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