AshMultiAccount.Phoenix.LiveHook (AshMultiAccount v0.1.1)

Copy Markdown View Source

LiveView on_mount hook for multi-account session resolution.

Determines whether the session is in multi-account mode and loads the appropriate :current_user and :primary_user assigns.

Usage

# In your router:
live_session :authenticated,
  on_mount: [
    {AshAuthentication.Phoenix.LiveSession, :load_from_session},
    {AshMultiAccount.Phoenix.LiveHook, {:load_multi_account, MyApp.Accounts.User}}
  ]

The hook name is a tuple {:load_multi_account, user_resource} so the user resource module is passed at the call site — no global application config needed.

Options

An optional keyword list can be passed as a third tuple element:

{AshMultiAccount.Phoenix.LiveHook, {:load_multi_account, MyApp.Accounts.User, sign_out_path: "/logout"}}
  • :sign_out_path — path to redirect to on fatal errors (default: "/sign-out").

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)

Standard Mode

When no multi-account session is detected, the hook loads :current_user from socket assigns or the session, applies configured display_fields, and sets :primary_user to nil.

Error Handling

  • If the primary user is not found (e.g., stale session after data reset), the hook falls back to standard mode
  • If the primary user is not active, the hook halts and redirects to the configured sign_out_path (default: "/sign-out")
  • If loading fails with an unexpected error, the hook halts with a generic error message

Summary

Functions

on_mount(arg, params, session, socket)

@spec on_mount(
  {:load_multi_account, module()} | {:load_multi_account, module(), keyword()},
  map(),
  map(),
  Phoenix.LiveView.Socket.t()
) :: {:cont | :halt, Phoenix.LiveView.Socket.t()}