dongwooklee96

dongwooklee96

How to create an associated resource via token in ash_authentication

When I have a user resource and an associated post resource.
I want the user_id to be automatically included in the post
when I log in and create a post with token. How is this possible?

For your information, I am using ash_graphql.

I would like to send a request when I create a post with a token that was created when I log in and I would like to be able to identify the user in the post through that token.

user

defmodule Dentallog.Accounts.User do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer,
    extensions: [
      AshAuthentication]
...
authentication do
    api Dentallog.Accounts

    strategies do
      password :password do
        identity_field(:email)
        hashed_password_field(:hashed_password)
        sign_in_tokens_enabled?(true)

        resettable do
          sender(Dentallog.Accounts.User.Senders.SendPasswordResetEmail)
        end
      end
    end
 

post

create :create do
      argument :photos, {:array, :map}, allow_nil?: true
      argument :hashtags, {:array, :map}, allow_nil?: true
      argument :category_id, :uuid

      change set_attribute(:dental_lab_category_id, arg(:category_id))

      change manage_relationship(:photos, type: :create)
      change manage_relationship(:hashtags, type: :create)
 end

...
relationships do
    belongs_to :user, Dentallog.Accounts.User do
      api Dentallog.Accounts
end

Marked As Solved

jimsynz

jimsynz

Ash Core Team

That looks correct. load_from_bearer should decode the token and place the current user in the assigns. If you’re trying to use the user as the actor then you will need to use set_actor.

Also Liked

zachdaniel

zachdaniel

Creator of Ash

In GraphQL you can request the user { id } if you add a graphql type to the user. However, exposing the user record just for that is generally not preferred.

In your case, you’ll want to do the following things:

  1. add to your :create action:
change relate_actor(:user)
  1. define the user_id attribute manually so that it is public
attribute :user_id, :uuid, allow_nil?: false, writable?: false
dongwooklee96

dongwooklee96

I solve this problem
with below code.
router.ex

pipeline :api do
    plug(:accepts, ["json"])
    plug(:load_from_bearer)
    plug(:set_actor)
    plug(AshGraphql.Plug)
  end

  scope "/" do
    pipe_through([:api])

    forward("/gql", Absinthe.Plug, schema: DentallogWeb.Schema)

    forward(
      "/playground",
      Absinthe.Plug.GraphiQL,
      schema: DentallogWeb.Schema,
      interface: :playground
    )
 end


def set_actor(conn, _opts) do
    conn
    |> Ash.PlugHelpers.set_actor(conn.assigns.current_user)
end

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement