wintermeyer

wintermeyer

Fetch a user by email address

Setup:

mix igniter.install ash_authentication_phoenix
mix ash_authentication.add_strategy password

I create a user in the web application. Now I want to access that user in the iex. How can I do that? This doesn’t work:

iex(5)> ExampleApp.Accounts.User |> Ash.Query.filter(email = "bob@example.com") |> Ash.read!()
** (Ash.Error.Invalid) Invalid Error

* No primary action of type :read for resource ExampleApp.Accounts.User, and no action specified

The resource includes this code:

    read :get_by_email do
      description "Looks up a user by their email"
      get? true

      argument :email, :ci_string do
        allow_nil? false
      end

      filter expr(email == ^arg(:email))
    end

How can I use that in the iex to fetch a single user by his email address?

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

Oh, right :thinking: I forgot that we generated that action in the auth generators TBH :sweat_smile: I thought it was a custom action you had written.

You can use that existing action. In that case, the only thing you need is this:

resource ExampleApp.Accounts.User do
  define :get_by_email, args: [:email]
end

Generally speaking, making an action just to get something by a given field is not necessary, because code interfaces have get_by option, and you can use Ash.get as well. However, in the case of AshAuthentication it needs to be guaranteed to have an action whose name it knows in order to look up users for authentication. You’re free to use that generated action for this as well :+1:

Also Liked

zachdaniel

zachdaniel

Creator of Ash

There are examples of using Ash.get in the read actions guide:

The most idiomatic way to do this is as follows:

Add the default read

# lib/accounts/user.ex

actions do
  defaults [:read]
end

Add a code interface on the domain

# lib/accounts.ex
resource ExampleApp.Accounts.User do
  define :get_user_by_email, action: :read, get_by: [:email]
end

Call it

ExampleApp.Accounts.get_by_email!("me@example.com", authorize?: false)
zachdaniel

zachdaniel

Creator of Ash

Relevant docs on primary actions: Actions — ash v3.29.3

I considered making the generators add a primary read action, but users is a pretty sensitive resource and I wanted to add only the minimal required to power authentication.

You will also need to add authorize?: false to your call because of the policies on users. A mild inconvenience, but the security benefits are worth it :slight_smile:

So after adding a primary action: Ash.get!(User, <id>, authorize?: false)

Where Next?

Popular in Questions Top

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement