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
Also Liked
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:
- add to your
:createaction:
change relate_actor(:user)
- define the
user_idattribute manually so that it is public
attribute :user_id, :uuid, allow_nil?: false, writable?: false
1
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
1
Popular in Questions
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
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
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
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
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
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
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
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
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
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
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
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
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









