webspaceadam

webspaceadam

How to use generated phoenix auth user as a resource within an ash relationship?

Hey guys :slight_smile:
I am quite new to elixir and super fresh in ash.

I already have an app built with phoenix and wanted to use ash with it now. I already have all the auth i need thanks to the gen.auth command. Everything works great and i got a poc working without ash.

Now i want to model my domains with ash and use the benefits it brings to the table.

I have a tenant resource that belongs to a user and this user is the owner. So i tried to model it according to the docs:

defmodule App.Tenant do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer

  postgres do
    table "tenants"
    repo App.Repo
  end

  attributes do
    uuid_primary_key :id
    attribute :name, :string
  end

  relationships do
    belongs_to :owner, App.Users.User
  end

  actions do
    defaults [:read, :destroy, create: :*, update: :*]
  end
end

But this of course does not work as the User is not a Spark DSL Entity/Resource…

Maybe a noob question and possibly i misread something in the docs, but i thought i do not have to use ash_auth and can still use the phoenix generated auth solution.

So how do i work with this?

  • do i have to implement ash auth to make it work?
  • do i have to extend the user module with some spark dsl sprinkle magic to make it work?
  • can i define the relationship in another way? i mean it is basically a helper for creating a cross table, right?

Thanks in advance for any help :slight_smile:

Marked As Solved

frankdugan3

frankdugan3

You can use the generated Phoenix auth in at least two ways:

  1. Refactor the Ecto schemas into Ash resources and the auth contexts to Ash actions.
  2. Duplicate the user Ecto schema as an Ash resource, and use that as the actor.

The actor in Ash is just a map, so you could just pass it the Ecto version of the user, but (I think) it will have a lot of limitations. For example, policies that leverage things like relates_to_actor_via etc and other relationship-related things won’t work.

There is a not currently a way to sprinkle some magic onto an Ecto schema to turn it into an Ash resource, but it has been discussed in the past (actually, moreso the other way around, Ash → Ecto). But there are some tricky problems regarding doing that, so I think it’s not really planned at the moment.

Also Liked

webspaceadam

webspaceadam

So far that seems to work! Now my app compiles and when registering users everything works fine :slight_smile:

kamaroly

kamaroly

I am afraid you may have two options:

  1. Use Ash authentication (I recommend this).
  2. Create two different Repos. One for Ash another for the traditional Ecto.

If you go with the first option and your app has unit tests for protected route, you will have to also change the way you authenticate the user to the following

      def login(conn, username \\ "test@example.com") do
        {:ok, user} = create_user(username)
        
        conn
        |> Phoenix.ConnTest.init_test_session(%{})
        |> put_session(:user, "user?id=#{user.id}")
      end
zachdaniel

zachdaniel

Creator of Ash

The relates_to_actor_via should actually continue to work, because we just grab the appropriate destination attributes from the user. If you want to relate a resource to your “user”, it will have to be a User. With that said, you can have an ecto schema for User and an Ash.Resource for User just fine :slight_smile:

Last Post!

webspaceadam

webspaceadam

So far that seems to work! Now my app compiles and when registering users everything works fine :slight_smile:

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New

We're in Beta

About us Mission Statement