webspaceadam

webspaceadam

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:

Showing Posts 1 to 5

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
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.

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:

webspaceadam

webspaceadam OP

This sounds very promising. I will try that and will come back when i got it working. Thanks a lot!

webspaceadam

webspaceadam OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews