lardcanoe
How to switch tenant of a logged in user
I want to give the user a tenant switcher, but am not sure what AshAuthentication function to call to do that. My main concern is that I want the current tenant to also be in the “session” not just some Process dictionary like Ash.set_tenant would do. I guess to complicate things even further, I would prefer the “actor” that Ash uses to be the UserTenant resource, but it doesn’t make sense to define authentication in that resource.
Setup: Phoenix 1.7 LiveView + AshAuthentication, w/ context multitenancy
Resources: “User” (has auth pw strategy), and then a “Tenant” and a “UserTenant”, with the “UserTenant” containing extra info, like which role the user has in the current tenant, which impacts policies and such.
AshAuthentication logs in the user, and right now I am selecting the default “UserTenant” in the “success” callback.
Went through ash_authentication v4.14.1 — Documentation and didn’t find anything about this.
Ideally Multitenancy — ash v3.29.3 would cover a user being in multiple tenants.
Thank you!
First Post!
zachdaniel
You should be able to do this ![]()
Is the idea to log them into a default tenant to start, and then provide a switcher? Or some kind of intermediate step, i.e after the user logs in, if they are in multiple tenants, redirect to a page where they can select the tenant to log in with?
I don’t think I would necessarily want the actor to be UserTenant, but I can see what you’re getting at. The main thing is that the tenant you’re in should be available pretty much everywhere, so you’d always be able to get the tenant, but also all queries and mutations should already be scoped to the tenant.
I would suggest doing something like this:
- if a user only has one UserTenant on login, set that and redirect to the proper place
- if they have multiple redirect to a page that lets them select a UserTenant
- Once that is set, set it into the session
- pass the tenant around in all of your queries, in addition to the actor
Small note: The process dictionary features will be removed in 3.0.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









