How to model context based multitenancy?
What would be the best approach to load the tenant info into the user when working with context base multitenancy?
- If I use
global? true
I can log in without knowing the tenant, but I'm not sure what the best strategy to fetch the tenant into after - or during - sign in, given the current_user has no tenant data
- If I use global? false
I need the tenant info before sign in, which can be work. I just need to figure it out how to build the magic link with the tenant subdomain - probably a phoenix question though.
Should I define my user resource as?
The context multitenancy docs is not clear how this should be mapped.
Also, if I don't add the attribute id and use global? true
is there a way to tell ash authentication to load the tenant info it used to log the user in?3 Replies
The attribute isn't used for context based multitenancy.
When you say "load the tenant info" what do you mean?
Do you mean how to get the organization loaded onto the user? If so, you'd want to do that in a plug of your own generally
You could use
Ash.load
or just %{user | organization: socket.assigns.organization}
(assuming you've fetched it somehow) for example
The tenant is given as an opt
to the magic link senderDo you mean how to get the organization loaded onto the user?exactly
The tenant is given as an opt to the magic link senderI figured, but I don't get the organization loaded into the context when I click the magic link to sign in. And because of that, I end up with a new record being created in the public.users table. So, what I'm thinking in doing is changing the link sent to the user, so that it would include the org subdomain in the url. For example
https://tenant-sub-domain.myapp.com/...
instead of https://myapp.com/...
.
That way I can use the subdomain to load the organization data into the context using a Plug
but I'm not sure if this is the right way to go about it, or if there is a better - or recommended - approach to this problemSolution
Yes you should likely do that 🙂