Create an Org on public schema and initial User on Org schema
I want to register an organization and an initial user at the same time. Creating
an organizations automatically creates and apply migrations to the organization schema.
The organization resource is global, so it's is saved on the public schema. The user should be saved on the
organization schema.
Steps(implementation in Ecto):
1. Generate a random string to be used as the organization schema name.
2. Create a schema with the random string as the schema name and apply migrations.
3. Create the organization on the public schema.
4. Create the user on the organization schema.
5. Save a user email and organization id to the "Lookup" table on the public schema.
How can I implement this in Ash?
Below is a similar implementation in Ecto:
"""
How can I implement this in Ash when using AshAuthentication?
I find it easy to reason about implementation generated by
phx.gen.auth
but I'm not sure where I can plug in a custom implementation as shown above.
Hope this makes sense. Thanks in advance.7 Replies
That question covers a bit of ground 😄
There are a few things you'll want. 1.
ash_postgres
has options on a resource called manage_tenant
Or even just template [:schema_name]
If you put that in your organization resource, then anytime an organization is created or updated, the schema will be managed
(only if schema_name
changed)
Then in your create action, you'd set schema_name
to your random string, and ash_postgres
would do the rest
Then you'd have an action on organization like this:
and you could create your lookup table as well there
So combining hooks on a custom create action + manage_tenant
should do what you want 🙂And then you'll want to follow the above guide for migrations and the like
So what can an implementation in
register_your_user_here(..., tenant: org.schema_name)
look like?
Is it right to do it this way:
although the above code is raising an error:
You likely just need to update ash_postgres
and ash
that bug was fixed in the last few weeks
And yeah, it would look very similar to that 😄
Alright 😁 , let me do that, will report back
Updated the deps, am now able to create a User using
:register_with_password
action🥳