Can not access to actor from token in create action

Hi sorry, I think I might be making a silly mistake somewhere. As you can see, I don’t have access to the actor in the create action — it’s always nil. However, I don’t have this issue in the read action, where I can access the actor without any problems.
create :register_with_password do
description "Register a new user with a email and password."
....

change set_attribute(:email, arg(:email))

change fn changeset, context ->
current_tenant = changeset.tenant
context.actor |> IO.inspect(label: "=-=--==-=-=-=-")

if current_tenant do
Ash.Changeset.change_attribute(changeset, :site_id, current_tenant)
else
changeset
end
end

validate MishkaCms.Accounts.Validations.MasterUserCreation

change {MishkaCms.Accounts.Changes.EnforceMasterEmailSeparation, []}

....
end
create :register_with_password do
description "Register a new user with a email and password."
....

change set_attribute(:email, arg(:email))

change fn changeset, context ->
current_tenant = changeset.tenant
context.actor |> IO.inspect(label: "=-=--==-=-=-=-")

if current_tenant do
Ash.Changeset.change_attribute(changeset, :site_id, current_tenant)
else
changeset
end
end

validate MishkaCms.Accounts.Validations.MasterUserCreation

change {MishkaCms.Accounts.Changes.EnforceMasterEmailSeparation, []}

....
end
But works inside my read action and i can print actor
read :me do
description "Get the current authenticated user"
get? true

filter expr(id == ^actor(:id))

prepare fn query, _context ->
query
|> Ash.Query.load([:permissions, :permission_values])
end
end
read :me do
description "Get the current authenticated user"
get? true

filter expr(id == ^actor(:id))

prepare fn query, _context ->
query
|> Ash.Query.load([:permissions, :permission_values])
end
end
I do not know why? Thank you in advance and sorry again
Solution:
i think i found it i am deleting it in my custom plug
Jump to solution
9 Replies
sevenseacat
sevenseacat2mo ago
access to the actor, or the tenant?
Shahryar
ShahryarOP2mo ago
Actor context.actor |> IO.inspect(label: "=-=--==-=-=-=-")
sevenseacat
sevenseacat2mo ago
ah I missed that. how are you calling the action?
Shahryar
ShahryarOP2mo ago
With json api
get :me, route: "/me"

post :register_with_password do
route "/register"

metadata fn _subject, user, _request ->
%{token: user.__metadata__.token}
end
end
get :me, route: "/me"

post :register_with_password do
route "/register"

metadata fn _subject, user, _request ->
%{token: user.__metadata__.token}
end
end
This get :me, route: "/me" can access to actor, but the post :register_with_password can not be able
sevenseacat
sevenseacat2mo ago
so if this is a typical registration action, there is no actor you're calling this as an unregistered user, to create an account
Shahryar
ShahryarOP2mo ago
Yes, but in my system admin and guest can create account!! i do not know why i can not be able to access actor in change or validation
change fn changeset, context ->
current_tenant = changeset.tenant
context.actor |> IO.inspect(label: "=-=--==-=-=-=-")

if current_tenant do
Ash.Changeset.change_attribute(changeset, :site_id, current_tenant)
else
changeset
end
end
change fn changeset, context ->
current_tenant = changeset.tenant
context.actor |> IO.inspect(label: "=-=--==-=-=-=-")

if current_tenant do
Ash.Changeset.change_attribute(changeset, :site_id, current_tenant)
else
changeset
end
end
I do not know where it deletes actor
sevenseacat
sevenseacat2mo ago
when the admin creates an account, I'm guessing also via the API, do they provide an authentication token? if not, there won't be an actor I don't think the actor is being deleted if there is an actor, is it in the changeset and not the context?
Shahryar
ShahryarOP2mo ago
Sorry As i said in the first question, my :me works and i pass the token and i can see the actor in it! but in create for register i have this problem
if there is an actor, is it in the changeset and not the context?
Unfortunately i can not see actor inside changeset and context
#Ash.Changeset<
domain: MishkaCms.Accounts,
action_type: :create,
action: :register_with_password,
attributes: %{email: #Ash.CiString<"mona13@mishka.life">},
relationships: %{},
arguments: %{
password: "**redacted**",
email: #Ash.CiString<"...">,
password_confirmation: "**redacted**"
},
errors: [],
data: %MishkaCms.Accounts.User{
id: nil,
email: nil,
...
},
valid?: true
>
#Ash.Changeset<
domain: MishkaCms.Accounts,
action_type: :create,
action: :register_with_password,
attributes: %{email: #Ash.CiString<"mona13@mishka.life">},
relationships: %{},
arguments: %{
password: "**redacted**",
email: #Ash.CiString<"...">,
password_confirmation: "**redacted**"
},
errors: [],
data: %MishkaCms.Accounts.User{
id: nil,
email: nil,
...
},
valid?: true
>
I think some place i am deleting this, idk :thinkies:
Solution
Shahryar
Shahryar2mo ago
i think i found it i am deleting it in my custom plug

Did you find this page helpful?