How can I set the actor globally after the user successfully logs in, so that I don’t have to pass actor in every query.
Here 's what I did so far. It works if I manually set actor while querying, but it does not work with the actor set in the plugs.
I added set_actor
in the Phoenix router plugs like the following
defmodule KamaroWeb.Router do
use KamaroWeb, :router
use AshAuthentication.Phoenix.Router
import AshAuthentication.Plug.Helpers # <-- ADDED THIS
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout, {KamaroWeb.Layouts, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
plug :load_from_session
plug :set_actor, :user # <-- ADDED THIS LINE
end
I inspected and indeed confirmed that the actor is being set in the connection privates.
However when I inspect the actor from the Policy, it is nil.
defmodule Kamaro.Checks.Can do
use Ash.Policy.SimpleCheck
def describe(_opts) do
"Check if a user/ actor has permission on a specific resource"
end
def match?(actor, context, opts) do
dbg(actor) # <-- ACTOR IS NIL HERE
{:ok, true}
end
end
But, when I query by passing the actor like the following, the actor is not nil.
Stock.get_categories!(actor: user)
Here is how the resource policy is configured
policies do
policy action_type(:read) do
authorize_if Kamaro.Checks.Can
end
end