sezaru

sezaru

Getting nil as actor in SimpleCheck during AshAuthentication sign-in

My user resource thas the following relationship:

    many_to_many :schools, School do
      through UserSchool
      join_relationship :user_schools

      source_attribute_on_join_resource :user_id
      destination_attribute_on_join_resource :school_id
    end

In my School resource, I have the following policy:

  policies do
    alias School.Checks

    policy always() do
      authorize_if Checks.IsConfirmed
    end
  end

And the IsConfirmed check is implemented as this:

defmodule Core.Ash.Checks.IsConfirmed do
  @moduledoc false

  use Ash.Policy.SimpleCheck

  def describe(_), do: "actor account is confirmed"

  def match?(%{confirmed_at: nil}, _, _), do: false
  def match?(%{confirmed_at: _}, _, _), do: true
end

After upgrading to Ash 3.0, when I try to sign-in using the sign_in_with_password action, the actor field from match?/3 always arrive as nil which will fail the match of the check.

Did something changed that I need to update to make this work again? I check both Ash upgrade guide and Ash authentication one but didn’t find anything that would help.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

The first correction you made is correct, the second one is not.

prepare fn query, _ -> 
  load_statement = [...]
  if query.action.name == :sign_in_with_password do
    Ash.Query.after_action(query, fn query, [user] = results -> 
      Ash.load(user, load_statement, actor: user)

      query, results ->
        # I don't actually think you need to do this, but `ash_authentication`
        # would translate the "multiple users matching" scenario into an error
        # so if you get more than one result you can ignore it
        {:ok, results}
    end)
  else
    Ash.Query.load(query, load_statement)
  end
end

Also Liked

sezaru

sezaru

Makes sense.

Thanks a bunch Zach, that worked great!

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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