This is an Ash and Hologram question, though I suppose it’s more of an Ash question sooooo:
For anyone using Hologram with Ash, how do you go about fetching the current user? I cobbled together something with help from The Machine with the preparation. It looks like:
# domain
resource MyApp.Accounts.User do
define :get_by_token, action: :get_by_token, args: [:token]
end
#resource
read :get_by_token do
argument :token, :string, allow_nil?: false
get? true
prepare MyApp.Accounts.FilterByToken
end
policy action(:get_by_token) do
authorize_if always()
end
# preparation
@impl true
def prepare(query, opts, context) do
token = Ash.Query.get_argument(query, :token)
case AshAuthentication.Jwt.verify(token, MyApp.Accounts.User) do
{:ok, claims, _} ->
query
|> Ash.Query.set_argument(:subject, claims["sub"])
|> AshAuthentication.Preparations.FilterBySubject.prepare(opts, context)
{:error, _} ->
Ash.Query.filter(query, false)
end
end
Then I do MyApp.Accounts.get_by_token(server.session["user_token"]) in my Hologram pages. Does anyone do anything drastically different/simpler? Is this approach all right?






















