<!--
This file was generated by Spark. Do not edit it by hand.
-->
# AshMultiAccount
An Ash extension for multi-account linking and switching.
Add this extension to your User resource to enable multi-account capabilities.
Users can link multiple accounts together and switch between them without
re-authenticating — similar to Google/Apple's account switcher UX.
## Concepts
- **Primary User**: The account that initiated the multi-account session
- **Linked User**: An additional account linked to the primary user
- **Session Token**: A UUID that groups all linked accounts within a browser session
- **Session-scoped**: Links exist per browser session, not globally
## Usage
```elixir
defmodule MyApp.Accounts.User do
use Ash.Resource,
domain: MyApp.Accounts,
data_layer: ..., # any Ash data layer (AshPostgres, AshSqlite, ETS, etc.)
extensions: [AshMultiAccount]
multi_account do
linked_account_resource MyApp.Accounts.LinkedAccount
display_fields [:name, :email, :avatar_url]
max_linked_accounts 5
end
end
```
## Generated Schema
The extension adds to your User resource:
- **Calculation** `:linked_accounts` — resolves linked account records for a given `session_token` argument
- **Action** `:get_user_with_linked_accounts` — a `get?` read action that accepts `primary_user_id` and `session_token` arguments, looks up the user by `id`, loads the configured `display_fields`, and loads the `linked_accounts` calculation for the given session
## Companion Resource
You must also create a LinkedAccount resource using the
`AshMultiAccount.LinkedAccount` extension. See its documentation for details.
## multi_account
Configure multi-account options for the User resource.
### Options
| Name | Type | Default | Docs |
|------|------|---------|------|
| [`linked_account_resource`](#multi_account-linked_account_resource){: #multi_account-linked_account_resource .spark-required} | `module` | | The LinkedAccount resource module. |
| [`active_check`](#multi_account-active_check){: #multi_account-active_check } | `{atom, any}` | | A `{field, value}` tuple used to filter linked users by an "active" status, e.g. `{:status, :active}`. |
| [`display_fields`](#multi_account-display_fields){: #multi_account-display_fields } | `list(atom)` | `[]` | Fields to load on users when fetching linked accounts for the switcher UI, e.g. `[:name, :email, :avatar_url]`. |
| [`max_linked_accounts`](#multi_account-max_linked_accounts){: #multi_account-max_linked_accounts } | `pos_integer` | `5` | Maximum number of linked accounts allowed per session. |
| [`get_user_with_linked_accounts_action_name`](#multi_account-get_user_with_linked_accounts_action_name){: #multi_account-get_user_with_linked_accounts_action_name } | `atom` | `:get_user_with_linked_accounts` | The name of the action that loads a user with their linked accounts. |
| [`linked_accounts_calculation_name`](#multi_account-linked_accounts_calculation_name){: #multi_account-linked_accounts_calculation_name } | `atom` | `:linked_accounts` | The name of the linked_accounts calculation on the User resource. |
<style type="text/css">.spark-required::after { content: "*"; color: red !important; }</style>