Fetching the current user with Ash + Hologram

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?

I’m pretty sure AshAuthentication provides actions out of the box for this. Not sure if you generated your app after we made the generators add actions to the resources, but there is, for example, an action for getting a user by token etc.

I haven’t used hologram yet so I can’t speak to it much more beyond that.

Hey, thanks for the reply! I generated it a couple of days ago. It has get_by_subject which I’m piggy-backing off of here but I could not find anything where you can just pass the token straight from the session and get a user back. I can keep looking and report back.