benswift

benswift

I’ve got an AshPhoenix app which needs to make authenticated HTTP requests to other APIs. I’m using Req.

These requests need an auth token, which I’m currently storing as a field on the User resource (the resource was originally created as part of the AshAuthentication setup).

The req “call site” is several layers deep in application code, and I’d like to avoid having to pass the auth/user info through all those layers if at all possible. So I’d like to be able to pull the current_user assign from the current session as close as possible to where I create (and perform) the Req.Request.

I’ve tried Ash.PlugHelpers.get_context as a :plug option in Req, but that returns nil because that’s the conn that Req is using, not the conn from the current user session.

There used to be (in Ash v2) a way of getting the current context (including actor from the process dict, but it doesn’t seem like that’s possible anymore? is there an alternative?

Sorry that’s all kindof abstract. I can fake up some minimal examples if you need me to.

Showing Posts 1 to 4

jackalcooper

jackalcooper

Does the library you are using support putting a custom client? I was trying to do some response rewrite for the library InstructorLite that is using req, so I add something like

defmodule MyClient do
  def post(url, opts) do
    case Req.post(url, opts) do
      ... rewrite response
      ... you can get the pid here I guess

and set the custom client option adapter_context: [http_client: MyClient,

benswift

benswift OP

Yeah so you mean create a custom client that puts/gets stuff from the process dict like the old “Store Process in Context” stuff used to do?

I tracked down the docs page for that stuff, it was deleted in d55864713a32 but the commit message didn’t have a specific rationale - I presume it was just a general “using process dicts for state is gross and probably makes parallel testing hard” vibes.

Anyway, thanks for your idea - I’m honestly thinking that the best option is to rearchitect my code so that there’s fewer layers of indirection between the action (where I do know the actor) and the code that creates the Req.Request struct (where I need the auth token).

zachdaniel

zachdaniel

Creator of Ash

This is what I’d recommend.

benswift

benswift OP

Thanks. That’s what I did. As an added bonus testing is easier :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

code-anth
Hello. I have benchmarked the JSON module ( JSON — Elixir v1.20.4 ) against a few libraries, including Erlang’s :jiffy. I’ve noticed a pa...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews