swrenn

swrenn

Using Ash/Phoenix with a bespoke auth backend

How do you recommend I connect an Ash/Phoenix app to a bespoke auth backend? The backend provides an endpoint that takes an email and password and returns a session key, and an endpoint that takes the session key and returns roles. The Ash/Phoenix app will provide an email/password login screen and store the session key for use in API calls to a resource server. Is ash_authentication a good fit for this use case? Or do I wrap the backend in an Ash resource and write a plug? For context, I’m new to Ash, Phoenix, Elixir, and web apps, in general, so there are gaps in my understanding.

Most Liked

zachdaniel

zachdaniel

Creator of Ash

AshAuthentication could be a good fit, but ultimately all that you need to do to make things work is to “get an actor however you want”, i.e in a plug, and assign it with Ash.PlugHelpers.set_actor/1. So you can definitely do something without AshAuthentication.

zachdaniel

zachdaniel

Creator of Ash

In your case, while I would probably create a resource with a single generic action for authenticating the user from the external service, I would likely not attempt to wrap the external API “generically” in a resource.

For example:

defmodule MyApp.Accounts.Authentication do
  use Ash.Resource

  action :authenticate, :struct do
    constraints instance_of: MyApp.Accounts.User

    argument :email, :string, allow_nil?: false
    argument :password, :string, allow_nil?: false

    run fn input, _ -> 
     case Req.get(....) do
       {:ok, ...} -> MyApp.Accounts.get_user_by_id(....)
     end
    end
  end
end

The custom authentication strategy could be a viable route, but ultimately it seems to me like what you are doing is so simple as to be better fit on the “edge”, i.e a simple plug who calls an action that ultimately retrieves the actor. Doing stuff with plugs or at the ingress layer of whatever you’re doing is totally fine and normal in an Ash application :smile:

zachdaniel

zachdaniel

Creator of Ash

There are a lot of potential ways to do it, like perhaps it’s a read action on the user resource that fetches the necessary info and then filters the query. As long as the logic is modeled as an action, generic or otherwise, then you’re doing it right as far as I’m concerned :smile:

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Other popular topics Top

New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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

We're in Beta

About us Mission Statement