caslu

caslu

How to create User Organization after registration in Ash Multi-tenancy?

Hello everyone, I’m building a collaborative editor where users can belong to multiple teams. I’ve decided to set up multi-tenancy on my user resource using the context strategy. So far, so good. However, Ash’s default behavior seems to require a team to be created before a user can be created and associated with it. This makes sense in general, but I need a different approach.

If you look at Notion, Figma, Vercel, Supabase, and many other applications with a “team” (organization, group, etc.) architecture, you’ll notice that they often register the user first. Then, the application creates a default organization, often using the user’s username, name, or other information.

Is there a way to adapt Ash’s expected flow to achieve this pattern? Something like:

User registration → Onboarding → Create user organization → Redirect to app home

I realize this is likely more of a design consideration than an Ash issue. If anyone has already tackled and solved this, I’d greatly appreciate any help. Thanks again, everyone! :smile:

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Ah, sorry was a bit distracted and didn’t realize this was the same thread.

You can create an organization right there in that handler if one doesn’t exist yet for that user, and then you can set the tenant on the action. If you want to do it transactionally in the case that something goes wrong.

action :send_magic_link_request do
  transaction? true
  argument :user_params, :map, allow_nil?: false

  run fn input, _ ->
    # get org for users email
    # if it doesn't exist, make it
    org = get_or_create_org(...)
    strategy = Info.strategy!(User, :magic_link)

    Strategy.action(strategy, :request, input.arguments.user_params, tenant: org)
  end
end

Then you can call that generic action from your controller.

Also Liked

zachdaniel

zachdaniel

Creator of Ash

My suggestion would be to create the users organization immediately on register, and then allow them to update/customize it after the fact. You can attach before/after action hooks to the register action for example.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement