daniglezl

daniglezl

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?

Showing Posts 1 to 10

zachdaniel

zachdaniel

Creator of Ash

Are you on the latest version of ash_authentication and ash_authentication_phoenix?

daniglezl

daniglezl OP

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", ...},
}
zachdaniel

zachdaniel

Creator of Ash

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:

daniglezl

daniglezl OP

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

krezicoder

krezicoder

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:

zachdaniel

zachdaniel

Creator of Ash

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.

jclement

jclement

I’m seeing the same thing in 3.x with the following versions.

  "ash": {:hex, :ash, "3.3.1", "fc67719590b3f3488f90b267666364f6ac364e7658bee3806c2739c9850d05d9", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8", [hex: :ets, repo: "hexpm", optional: false]}, {:igniter, ">= 0.3.11 and < 1.0.0-0", [hex: :igniter, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: true]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:reactor, "~> 0.9", [hex: :reactor, repo: "hexpm", optional: false]}, {:simple_sat, ">= 0.1.1 and < 1.0.0-0", [hex: :simple_sat, repo: "hexpm", optional: true]}, {:spark, ">= 2.2.8 and < 3.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}, {:stream_data, "~> 1.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "de8568f528194edd6d22f8941f5f67589788fe9a3868e900efac81e2ded25955"},
  "ash_authentication": {:hex, :ash_authentication, "4.0.1", "27e5fcda1022897a02903441a049ba9e5f655e51a757039d946f5bce1de0447c", [:mix], [{:ash, "~> 3.0", [hex: :ash, repo: "hexpm", optional: false]}, {:ash_postgres, "~> 2.0", [hex: :ash_postgres, repo: "hexpm", optional: true]}, {:assent, ">= 0.2.8 and < 1.0.0-0", [hex: :assent, repo: "hexpm", optional: false]}, {:bcrypt_elixir, "~> 3.0", [hex: :bcrypt_elixir, repo: "hexpm", optional: false]}, {:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:finch, "~> 0.18.0", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: false]}, {:joken, "~> 2.5", [hex: :joken, repo: "hexpm", optional: false]}, {:plug, "~> 1.13", [hex: :plug, repo: "hexpm", optional: false]}, {:spark, "~> 2.0", [hex: :spark, repo: "hexpm", optional: false]}, {:splode, "~> 0.2", [hex: :splode, repo: "hexpm", optional: false]}], "hexpm", "e204585c8eed2d46a12e7031da48a169c513d5074ba43da90be0a92f7e1e0413"},
  "ash_authentication_phoenix": {:hex, :ash_authentication_phoenix, "2.0.1", "572126105d5479e3dafd737951118dae559aa89bad71b0b06ad7aa09a395829e", [:mix], [{:ash, "~> 3.0", [hex: :ash, repo: "hexpm", optional: false]}, {:ash_authentication, "~> 4.0", [hex: :ash_authentication, repo: "hexpm", optional: false]}, {:ash_phoenix, "~> 2.0", [hex: :ash_phoenix, repo: "hexpm", optional: false]}, {:bcrypt_elixir, "~> 3.0", [hex: :bcrypt_elixir, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.6", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_html_helpers, "~> 1.0", [hex: :phoenix_html_helpers, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.18", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:slugify, "~> 1.3", [hex: :slugify, repo: "hexpm", optional: false]}], "hexpm", "b4b38c72cb49fd6c5243e4a110b1bcd4138bb0074bb8b5a165e40d29abc1382e"},
  "ash_phoenix": {:hex, :ash_phoenix, "2.1.0", "a05d372df10f079b96ff1558aa2fc896a064c6861a25d6cf419ef434a6607f80", [:mix], [{:ash, "~> 3.0", [hex: :ash, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.5.6 or ~> 1.6", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.20.3 or ~> 1.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}], "hexpm", "ef6a509db2a69ace5e8200fd5cb075f707980a2d6e29ac65b36ea215f3e8952c"},

I see the code is referring to current_tenant, is there something special I have to do to AshAuthentication to pass that in? My plug is currently calling set_tenant with my Tenant struct, and then I’ve implemented a to_tenant to suck the ID out of that.

 defmodule FloorwardWeb.Plugs.TenantPlug do
  import Plug.Conn
  def init(default), do: default

  def call(conn, _opts) do
    tenant = get_tenant_from_host(conn.host)

    conn
    |> Ash.PlugHelpers.set_tenant(tenant)
  end

  defp get_tenant_from_host(host) do
    case Floorward.Tenants.Tenant
         |> Ash.Query.for_read(:by_hostname, hostname: host)
         |> Ash.read_one() do
      {:ok, tenant} -> tenant
      _ -> :unknown
    end
  end
end
zachdaniel

zachdaniel

Creator of Ash

Is your TenantPlug definitely running before your authorization live session? like is the appropriate pipeline used in that scope, etc?

jclement

jclement

I sure think so:

From endpoint.ex

  plug Plug.MethodOverride
  plug Plug.Head
  plug Plug.Session, @session_options
  plug FloorwardWeb.Plugs.TenantPlug
  plug FloorwardWeb.Plugs.CurrentUriPlug
  plug FloorwardWeb.Router

And then in the router.ex:

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, html: {FloorwardWeb.Layouts, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug :load_from_session
  end

  scope "/", FloorwardWeb do
    pipe_through :browser

    get "/", PageController, :home
    sign_in_route(
      register_path: "/register",
      reset_path: "/reset",
      # prevent signed in users from hitting Signin
      on_mount: [
        {LiveUserAuth, :live_no_user}
      ],
      # https://hexdocs.pm/ash_authentication_phoenix/ui-overrides.html
      overrides: [FloorwardWeb.AuthOverrides, AshAuthentication.Phoenix.Overrides.Default]
    )

    sign_out_route AuthController
    auth_routes_for Floorward.Accounts.User, to: AuthController
    reset_route []

    ash_authentication_live_session :authentication_optional,
      on_mount: [
        {LiveUserAuth, :live_user_optional},
      ] do
      live "/customers/", Customers.CustomerListLive, :index
    end
  end

I feel like I’m missing something here because

<%= @current_tenant.name %> in my Customer live view does work.

zachdaniel

zachdaniel

Creator of Ash

:thinking: that looks right to me… As far as I can tell, we’re properly setting the tenant into the session, which is then being pulled out of the session and put into an assign in an on mount hook. Can you show the error you’re seeing? Just so we can be sure its the same?

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews