AshAuthentication Errors

minib
2023-01-30

minib:

After I’ve updated ash packages:

ash 2.5.9 => 2.5.12
ash_authentication 3.7.3 => 3.7.5
ash_authentication_phoenix 1.4.5 => 1.4.6
ash_phoenix 1.2.4 => 1.2.5
ash_postgres 1.3.3 => 1.3.5

I got this error:

minib:

[error] #PID<0.706.0> running Phoenix.Endpoint.SyncCodeReloadPlug (connection #PID<0.705.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /sign-in
** (exit) an exception was raised:
    ** (UndefinedFunctionError) function AshAuthentication.Phoenix.Components.Confirmation.__live__/0 is undefined (module AshAuthentication.Phoenix.Components.Confirmation is not available)
        AshAuthentication.Phoenix.Components.Confirmation.__live__()
        (phoenix_live_view 0.18.11) lib/phoenix_component.ex:1761: Phoenix.Component.live_component/1
        (phoenix_live_view 0.18.11) lib/phoenix_live_view/html_engine.ex:35: Phoenix.LiveView.HTMLEngine.component/3
        (ash_authentication_phoenix 1.4.6) lib/ash_authentication_phoenix/components/sign_in.ex:119: anonymous fn/2 in AshAuthentication.Phoenix.Components.SignIn.strategy/1
        (ash_authentication_phoenix 1.4.6) /Users/egor/projects/open_check/open_status/deps/ash_authentication_phoenix/lib/ash_authentication_phoenix/components/sign_in.ex:103: AshAuthentication.Phoenix.Components.SignIn.render/1
        (elixir 1.14.2) lib/enum.ex:1780: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3

minib:

And when I downgraded version back I got another error:

[error] #PID<0.2315.0> running Phoenix.Endpoint.SyncCodeReloadPlug (connection #PID<0.2300.0>, stream id 4) terminated
Server: localhost:4000 (http)
Request: GET /sign-in
** (exit) an exception was raised:
    ** (RuntimeError) found duplicate ID "sign-in-hr" for component AshAuthentication.Phoenix.Components.HorizontalRule when rendering template
        (phoenix_live_view 0.18.11) lib/phoenix_live_view/diff.ex:616: anonymous fn/5 in Phoenix.LiveView.Diff.render_pending_components/6
        (elixir 1.14.2) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
        (stdlib 4.1.1) maps.erl:411: :maps.fold_1/3
        (phoenix_live_view 0.18.11) lib/phoenix_live_view/diff.ex:609: Phoenix.LiveView.Diff.render_pending_components/6
        (phoenix_live_view 0.18.11) lib/phoenix_live_view/diff.ex:145: Phoenix.LiveView.Diff.render/3
        (phoenix_live_view 0.18.11) lib/phoenix_live_view/static.ex:252: Phoenix.LiveView.Static.to_rendered_content_tag/4
        (phoenix_live_view 0.18.11) lib/phoenix_live_view/static.ex:135: Phoenix.LiveView.Static.render/3
        (phoenix_live_view 0.18.11) lib/phoenix_live_view/controller.ex:39: Phoenix.LiveView.Controller.live_render/3
        (phoenix 1.7.0-rc.2) lib/phoenix/router.ex:425: Phoenix.Router.__call__/5
        (open_status 0.1.0) lib/open_status_web/endpoint.ex:1: OpenStatusWeb.Endpoint.plug_builder_call/2
        (open_status 0.1.0) lib/plug/debugger.ex:136: OpenStatusWeb.Endpoint."call (overridable 3)"/2
        (open_status 0.1.0) lib/open_status_web/endpoint.ex:1: OpenStatusWeb.Endpoint.call/2
        (phoenix 1.7.0-rc.2) lib/phoenix/endpoint/sync_code_reload_plug.ex:22: Phoenix.Endpoint.SyncCodeReloadPlug.do_call/4

Any ideas what went wrong? Will appreciate any suggestions.

minib:

I don’t have any overrides if it matters

ZachDaniel:

🤔 I think <@346791515923939328> will need to chime in

ZachDaniel:

Actually have you tried force recompiling after updating?

minib:

yes, I have tried that

ZachDaniel:

Kk. I’ll update ash_hq to the latest for those libraries and see if I get the same thing

ZachDaniel:

Yeah, I’m getting these errors too

ZachDaniel:

🤔

ZachDaniel:

I think I see where it is

ZachDaniel:

lemme confirm

ZachDaniel:

So for some reason:

AshAuthentication.Info.authentication_strategies(AshHq.Accounts.User)

is returning the addons as well

ZachDaniel:

and that is causing this issue

minib:

hmm

ZachDaniel:

Its definitely an underlying thing, will fix

ZachDaniel:

found it

ZachDaniel:

I just pushed to main, can you try that out? Of ash_authentication

minib:

sure

minib:

I still get

found duplicate ID "sign-in-hr" for component AshAuthentication.Phoenix.Components.HorizontalRule when rendering template

minib:

btw I also had to override ash_authentication , because of

Dependencies have diverged:
* ash_authentication (https://github.com/team-alembic/ash_authentication.git)
  the dependency ash_authentication in mix.exs is overriding a child dependency:

  > In mix.exs:
    {:ash_authentication, [env: :prod, git: "https://github.com/team-alembic/ash_authentication.git"]}

  > In deps/ash_authentication_phoenix/mix.exs:
    {:ash_authentication, "~> 3.5", [env: :prod, hex: "ash_authentication", repo: "hexpm", optional: false]}

  Ensure they match or specify one of the above in your deps and set "override: true"
** (Mix) Can't continue due to errors on dependencies

ZachDaniel:

Yeah, okay I think I see how that could be happening too

ZachDaniel:

🤔 do you have multiple resources that can be used to sign in?

minib:

like different kind of users?

ZachDaniel:

yeah

minib:

then just one

minib:

But I have both password and github

minib:

everything was fine a week ago

ZachDaniel:

Yeah, we made changes to the underlying library to support custom strategies

ZachDaniel:

Which is a great thing 🙂

ZachDaniel:

But there are a few kinks it seems

minib:

okay

ZachDaniel:

I suspect that the duplicate ids error was there in previous versions

ZachDaniel:

thats just a warning in the browser console right?

minib:

nope, that a proper error

ZachDaniel:

oh, really?

minib:

minib:

ZachDaniel:

gotcha

ZachDaniel:

Okay…does your user resource appear in multiple apis?

ZachDaniel:

The reason I ask is that the hr is defined once per resource it thinks you could authenticate as

ZachDaniel:

What does this return for you in iex

AshAuthentication.authenticated_resources(:your_otp_app)

jart:

Thanks for jumping in Zach. Sorry about leaving a mess. I’ll take a look at your fixes today and try and write tests to avoid regressions.

ZachDaniel:

There is still one more that I don’t understand, which is the duplicate horizontal rule

ZachDaniel:

I accidentally pushed the first fix straight to main though 😆

jart:

Nothing like a YOLO release to wake you up in the morning.

ZachDaniel:

This is why I do explicit releases in Ash

jart:

Hey it won’t release if it doesn’t build.

ZachDaniel:

true true

minib:

it does I guess, I had to add it to another API’s registry, where it was related to. Like MyApp.Accounts.User I’ve added to MyApp.Blog.Registry because of author relation in MyApp.Blog.Post otherwise it was compile time erroring Resources MyApp.Accounts.User must be included in the registry.

ZachDaniel:

Ah, okay

ZachDaniel:

So we can fix that

minib:

🙏

ZachDaniel:

but also you don’t actually have to add it to your other registry if you don’t want to

ZachDaniel:

I need to update the error message

minib:

oo

minib:

i felt it wasn’t right

ZachDaniel:

But you can do

belongs_to :author, MyApp.Accounts.User do
  api MyApp.Accounts
end

ZachDaniel:

You just need to configure which api to use if its not in the same one as the resource

minib:

  relationships do
    belongs_to :created_by, OpenStatus.Accounts.User do
      api OpenStatus.Accounts
      allow_nil? false
    end

    has_many :events, OpenStatus.Monitors.Event
  end

but i have this already

frankdugan3:

Yeah, that tripped me up the other day! I just started using split APIs now that it’s a lot easier to cross the boundaries w/ resources.

ZachDaniel:

🤔 that should be all you need to do

ZachDaniel:

Perhaps double check the error message after you do that?

ZachDaniel:

i.e remove user from the blog context

minib:

you mean registry? i just removed it from the registy

ZachDaniel:

yeah, sorry, registry 😆

frankdugan3:

I haven’t split up my large app yet, but I’m interested in seeing how much that improves compile times once I do.

minib:

then I get Resources OpenStatus.Accounts.User must be included in the registry

ZachDaniel:

🤔 🤔 🤔

ZachDaniel:

Do you have any other resources that point to that?

minib:

anyways everything was fine until I’ve updated dependencies to the latest, today

ZachDaniel:

🤔 I’m on all the latest and I don’t have that issue w/ crossing apis… I’ll push something up that provides a better error message and maybe that will help

minib:

i have two resources under OpenStatus.Monitors with relations to OpenStatus.Accounts.User

minib:

and so far there are only two API in my project, Accounts and Monitors

minib:

i thought it’s strange, but thought it’s for the sake of compiler optimization or something, was going to ask you later

minib:

i can share the repo with you if it would help

ZachDaniel:

I’ll push up an improvement to the error message in just a sec and it should tell us exactly what it thinks the issue is 🙂

minib:

cool, standing by

frankdugan3:

Meanwhile, are both the apis added to config.exs ? That’s easy to forget to do!

minib:

yup

config :open_status,
  ash_apis: [OpenStatus.Accounts, OpenStatus.Monitors]

minib:

like I said it was working last week

ZachDaniel:

That is very strange.

minib:

agree

ZachDaniel:

You didn’t make any changes to your resources?

minib:

no, I’ve set up the auth, logged-in and was working on liveviews, until today I’ve decided to update the deps

ZachDaniel:

oh, well

ZachDaniel:

That could be something to do with only doing a partial recompile while you’re working

minib:

i’ve changed the resources, last time last week, but it was ok until the deps update

ZachDaniel:

and so the registry didn’t recompile or something along those lines

ZachDaniel:

but when you pull latest ash, all the ash stuff has to recompile

ZachDaniel:

which is bad, it should have told you about this right away

ZachDaniel:

but I think that is the reason you didn’t see it before.

minib:

today I’ve also tried to remove _build dir and recompile the whole thing

ZachDaniel:

Yeah, so the idea here is that it was broken before, but the error wasn’t showing because of partial recompilation

ZachDaniel:

so clearing _build wouldn’t help

minib:

nope

ZachDaniel:

okay can you try main? I made the validation much simpler and had it produce a much better error message

minib:

main ash_auth? it’s in my mix.exs already, or you mean another package?

minib:

# mix.exs
      {:ash, "~> 2.5"},
      {:ash_postgres, "~> 1.3"},
      # {:ash_authentication, "~> 3.7"},
      {:ash_authentication, github: "team-alembic/ash_authentication", override: true},
      {:ash_authentication_phoenix, "~> 1.4"},

ZachDaniel:

Main ash

minib:

okay

ZachDaniel:

And you’d need to do mix deps.update ash_authentication to pull latest main of ash_authentication anyway (a common thing that trips people up, so just letting you know)

minib:

I’ll remove _build and deps

ZachDaniel:

Shouldn’t be necessary, but its fine if you do

minib:

got this

Dependencies have diverged:
* ash (https://github.com/ash-project/ash.git)
  the dependency ash in mix.exs is overriding a child dependency:

  > In mix.exs:
    {:ash, [env: :prod, git: "https://github.com/ash-project/ash.git"]}

  > In deps/ash_authentication/mix.exs:
    {:ash, "~> 2.5 and >= 2.5.11", [env: :prod, hex: "ash", repo: "hexpm"]}

  Ensure they match or specify one of the above in your deps and set "override: true"
** (Mix) Can't continue due to errors on dependencies

going to override

ZachDaniel:

Ah, yeah you’ll always have to do that if you set ash or any library that has something depending on it to a github dependency

minib:

ooo i guess the new error message works! 🙂

ZachDaniel:

🥳

minib:

so yeah I had another resouse in the api with a relation to User which didn’t specify Accounts api

ZachDaniel:

Got it, was hoping that was the issue 😆

minib:

the sign-in also works now!

minib:

huge thanks to all of you guys!

ZachDaniel:

I’ve made PRs to ash_authentication & ash_authentication_phoenix to solve it in the future so that if someone actually does have their user in multiple apis on purpose, they won’t encounter the issue that you did

ZachDaniel:

Glad to help! Thank you for helping us work out the kinks and being patient 🙂 Even more cool stuff is coming for ash_authentication over time which is really exciting.

minib:

can’t wait 🙂

minib:

marking the tread as resolved

ZachDaniel:

I beat you to it 😆

frankdugan3:

warning: Routes.module_offering_index_url/2 is undefined (module Routes is not available or is yet to be defined)
  lib/decentralize/learning/notifiers/learning_notifier.ex:22: Decentralize.Student.LearningNotifier.notify/1

** (EXIT from #PID<0.111.0>) an exception was raised:
    ** (RuntimeError) Could not determine api for Decentralize.Learning.ModuleOffering.facilitator. Please do one of the following:

1. Add the resource to the registry `Elixir.Decentralize.Learning.Registry`

2. Set the `api` option on the relationship, for example:


belongs_to :facilitator, Decentralize.Authentication.User do
  api SomeOtherApi
end

^ EXCELLENT error message, so much better! <:ashley:1017613763945443398>

ZachDaniel:

Some of these things are like no brainers once you realize it, but its hard to see when I’m so familiar with the issue

ZachDaniel:

but thats something that people encounter all the time, especially newer people

frankdugan3:

In my case, I moved stuff around in an existing app, was kind of hard to figure out which file was missing the API declaration. 🙂

frankdugan3:

Even though I knew that’s what it was. W/ this new error, no-brainer.

ZachDaniel:

⛔ 🧠