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
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
endGenerated Schema
The extension adds to your User resource:
- Calculation
:linked_accounts— resolves linked account records for a givensession_tokenargument - Action
:get_user_with_linked_accounts— aget?read action that acceptsprimary_user_idandsession_tokenarguments, looks up the user byid, loads the configureddisplay_fields, and loads thelinked_accountscalculation 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 | module | The LinkedAccount resource module. | |
active_check | {atom, any} | A {field, value} tuple used to filter linked users by an "active" status, e.g. {:status, :active}. | |
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 | pos_integer | 5 | Maximum number of linked accounts allowed per session. |
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 | atom | :linked_accounts | The name of the linked_accounts calculation on the User resource. |