lardcanoe

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

zachdaniel

Creator of Ash

You should be able to do this :slight_smile:

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:

  1. if a user only has one UserTenant on login, set that and redirect to the proper place
  2. if they have multiple redirect to a page that lets them select a UserTenant
  3. Once that is set, set it into the session
  4. 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.

Where Next?

Popular in Questions Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

We're in Beta

About us Mission Statement