How to create organization after user creation.

Ideally it would create organization for user after registration, but if user comes from invitation (to specific organization) than it does not create new one. No functionality is created yet, im not sure, how to even approach this. In user resource there isnt even actions for create. Apparently its definition happens somewhere in i suppose.
authentication do
api Domain.Accounts

strategies do
password :password do
identity_field :email
sign_in_tokens_enabled? true
end
end

tokens do
enabled? true
token_resource Domain.Accounts.Token
signing_secret Application.compile_env(:domain, DomainWeb.Endpoint)[:secret_key_base]
end
end
authentication do
api Domain.Accounts

strategies do
password :password do
identity_field :email
sign_in_tokens_enabled? true
end
end

tokens do
enabled? true
token_resource Domain.Accounts.Token
signing_secret Application.compile_env(:domain, DomainWeb.Endpoint)[:secret_key_base]
end
end
Sorry if i ask noob questions, i find docs rather confusing.
4 Replies
ZachDaniel
ZachDaniel•3y ago
Yep! So you'll probably need to make some of your own actions to make these kinds of things work. You will also probably need to write your own sign in liveview for the accept invitation flow You can attach behavior to the generated actions using the global changes
changes do
change CreateOrganization, where: [action(:register_with_password)]
end
changes do
change CreateOrganization, where: [action(:register_with_password)]
end
Then
defmodule CreateOrganization do
use Ash.Resource.Change

def change(changeset, _, _) do
Ash.Changeset.before_action(changeset, fn changeset ->
org = create_organization_for_user!()
Ash.Changeset.force_change_attribute(changeset, :organization_id, org.id)
end)
end
end
defmodule CreateOrganization do
use Ash.Resource.Change

def change(changeset, _, _) do
Ash.Changeset.before_action(changeset, fn changeset ->
org = create_organization_for_user!()
Ash.Changeset.force_change_attribute(changeset, :organization_id, org.id)
end)
end
end
actually yeah, use before_action 🙂 will update the example it will all happen in the same transaction For accepting an invite, you'll likely need your own actions/routes/liveview, and create a user on your own. You'll end up getting familiar with actions/arguments/changes as part of that exercise 🙂
roberts.gulans
roberts.gulansOP•3y ago
Question about this is, how i possible could find by my self that action for it is :register_with_password? Im a little bit confused about how to approach finding related parts.
ZachDaniel
ZachDaniel•3y ago
So thats one part documentation and one part "introspection" the docs part is that it should say somewhere in the docs for ash_authentication that by default you get a register_with_<name> action for each strategy i.e register_with_password comes from password :password do the introspection part is that if you boot up iex
resource
|> Ash.Resource.Info.actions()
|> Enum.map(&"#{&1.type}: #{&1.name})
resource
|> Ash.Resource.Info.actions()
|> Enum.map(&"#{&1.type}: #{&1.name})
That will show each action and its type

Did you find this page helpful?