sodapopcan

sodapopcan

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?

Most Liked

zachdaniel

zachdaniel

Creator of Ash

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.

sodapopcan

sodapopcan

Sorry for the long delay here.

I used the generator from ash-hq. I just generated a new app again today and there is still no get_by_token in the User resource, just a get_by_subject which still requires taking the steps above (although it can be simplified to pattern matching the subject out of the claims and using get_by_subject instead of the preparation as above).

EDIT: For a bit more context, I’m only using magic link auth atm.

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement