dongwooklee96
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
Trending in Questions
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
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:
:createaction:user_idattribute manually so that it is publicdongwooklee96
Thank you for your response.
However I encounted this error.
route.ex
It seems like the issue is related to the “get_actor_from_token” plug in the router. There might be a problem with the logic for extracting the user from the token. Is there a specific problem in my code?
I think it’s a little awkward to extract the token information generated by ash_authentication from guardian. I’d appreciate it if you could give me your opinion regarding it.
For your information, I referred to that document.
ref : Authorize with GraphQL — ash_graphql v1.9.4
zachdaniel
Ah, so that guide is showing an example configuration, not code meant to be used with
ash_authentication(in fact, it predatesash_authentication. The setup guide for integrating ash_authentication and phoenix show you how to set up your router in such a way that the actor should properly be made present:dongwooklee96
router.ex
So, if I use ash_authentication, I can write code like this on the router, and when I make a
graphql request and the user_id will be filled in?
Also on the action, I was wondering how I can get :current_user from the token.
req-header
post.ex
However, I was faced with the following error.
call stack
zachdaniel
Oh, sorry, you aren’t currently using
AshAuthentication? In that case, you’d have to be usingGuardiandirectly, at which point you’d need to check their documentation on how to extract the resource id from the token. Then, given that, you can use something likeYourApi.getto look up the user by id. Then you can useAsh.PlugHelpers.set_actor/2as you did in your original example.dongwooklee96
I am currently using ashAuthentication, and I am facing some difficulty in identifying the cause due to some declarative code in ash. I will attach my entire code for your reference. I would appreciate any relevant insights you could provide.
router.ex
Originally, I would have written the router with ash_authentication like this, correct?
user.ex
zachdaniel
Yeah, as far as I can tell, that setup looks correct.
:load_from_bearershould attach the current user. @jimsynz may have some insights here.jimsynz
That looks correct.
load_from_bearershould 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 useset_actor.zachdaniel
Ah, I didn’t realize the
set_actorpart.dongwooklee96
Hmm… It’s very weired thing…
I’m sure check with IO.inspect that the actor is set up, but I still get an error that the actor is not provided
private: %{
:ash => %{actor: :user},
DentallogWeb.Router => ,
:phoenix_router => DentallogWeb.Router,
:plug_session_fetch => function<1.76384852/1 in Plug.Session.fetch_session/1>,
:before_send => [function<0.54455629/1 in Plug.Telemetry.call/2>,
function<1.28079098/1 in Phoenix.LiveReloader.before_send_inject_reloader/3>],
:phoenix_endpoint => DentallogWeb.Endpoint,
:phoenix_format => “json”,
:phoenix_request_logger => {“request_logger”, “request_logger”}
},
router.ex
output