AshAuthentication does not use tenant set with Ash.PlugHelpers.set_tenant

Hi, I’m trying to get AshAuthentication to work in a context multi-tenancy setup but I’m having trouble getting AshAuthentication to use the tenant set using Ash.PlugHelpers.set_tenant/2. I have the following plug added to the pipeline:

defmodule ExampleWeb.Plugs.TenantSetter do
  @moduledoc """
  Set Ash tenant from current_tenant in connection
  """
  @behaviour Plug

  @impl true
  def init(opts), do: opts

  @impl true
  def call(conn, _opts) do
    Ash.PlugHelpers.set_tenant(conn, conn.assigns.current_organization.id)
  end
end

and I can see the tenant being added to conn but I get the following error when trying to authenticate:

[warning] Unhandled error in form submission for Example.Accounts.User.sign_in_with_password

This error was unhandled because Ash.Error.Invalid.TenantRequired does not implement the `AshPhoenix.FormData.Error` protocol.

** (Ash.Error.Invalid.TenantRequired) Queries against the Example.Accounts.User resource require a tenant to be specified

I’ve found other topics that mention that AshAuthentication should use the tenant when set with the PlugHelpers. Is this correct?

1 Like

Are you on the latest version of ash_authentication and ash_authentication_phoenix?

I am:

%{
  "ash": {:hex, :ash, "2.21.12", ...},
  "ash_authentication": {:hex, :ash_authentication, "3.12.4", ...},
  "ash_authentication_phoenix": {:hex, :ash_authentication_phoenix, "1.9.4", ...},
}

Ah, I wonder if we didn’t back port a fix somewhere:( I’m not at a computer, but you could fork ash authentication phoenix, and check out the tag for the version you’re on, make a branch, and cherry pick this commit: fix: set tenant on form creation · team-alembic/ash_authentication_phoenix@7430ab7 · GitHub

Then you can point at your fork. That will likely fix the issue.

Or you can open an issue and well fix it in the next day or two :slight_smile:

I see, thanks for looking into it, I’ll try this later tonight :slight_smile:

Hi @zachdaniel is this resolved ? Can I update to the latest version I am facing the same issue dont want to fork the repo & point to it for sanity reasons :slight_smile:

In 2.x? Or in 3.x? The problem doesn’t exist in 3.x, only in the 2.x compatible versions.

I didn’t address this issue, but if someone could make an issue or PR w/ the steps I mentioned above that would be great.