Phoenix 1.18 Ash authentication error

I'm seeing the following error when I try to access /sign-in or /register on a new application I'm playing with.
[error] ** (RuntimeError) Cannot set assign `current_user` before default `AshAuthentication.Phoenix.LiveSession.on_mount/4` has run.
(ash_authentication_phoenix 2.7.0) lib/ash_authentication_phoenix/live_session.ex:163: anonymous fn/5 in AshAuthentication.Phoenix.LiveSession.on_mount/4
(elixir 1.18.3) lib/enum.ex:4515: anonymous fn/3 in Enum.reduce/3
(elixir 1.18.3) lib/stream.ex:1761: anonymous fn/3 in Enumerable.Stream.reduce/3
(elixir 1.18.3) lib/enum.ex:4968: Enumerable.List.reduce/3
(elixir 1.18.3) lib/stream.ex:1773: Enumerable.Stream.do_each/4
(elixir 1.18.3) lib/enum.ex:4515: Enum.reduce/3
(ash_authentication_phoenix 2.7.0) lib/ash_authentication_phoenix/live_session.ex:159: AshAuthentication.Phoenix.LiveSession.on_mount/4
(phoenix_live_view 1.0.17) lib/phoenix_live_view/lifecycle.ex:158: anonymous fn/4 in Phoenix.LiveView.Lifecycle.mount/3
(phoenix_live_view 1.0.17) lib/phoenix_live_view/lifecycle.ex:237: Phoenix.LiveView.Lifecycle.reduce_socket/3
(phoenix_live_view 1.0.17) lib/phoenix_live_view/utils.ex:346: anonymous fn/6 in Phoenix.LiveView.Utils.maybe_call_live_view_mount!/5
[error] ** (RuntimeError) Cannot set assign `current_user` before default `AshAuthentication.Phoenix.LiveSession.on_mount/4` has run.
(ash_authentication_phoenix 2.7.0) lib/ash_authentication_phoenix/live_session.ex:163: anonymous fn/5 in AshAuthentication.Phoenix.LiveSession.on_mount/4
(elixir 1.18.3) lib/enum.ex:4515: anonymous fn/3 in Enum.reduce/3
(elixir 1.18.3) lib/stream.ex:1761: anonymous fn/3 in Enumerable.Stream.reduce/3
(elixir 1.18.3) lib/enum.ex:4968: Enumerable.List.reduce/3
(elixir 1.18.3) lib/stream.ex:1773: Enumerable.Stream.do_each/4
(elixir 1.18.3) lib/enum.ex:4515: Enum.reduce/3
(ash_authentication_phoenix 2.7.0) lib/ash_authentication_phoenix/live_session.ex:159: AshAuthentication.Phoenix.LiveSession.on_mount/4
(phoenix_live_view 1.0.17) lib/phoenix_live_view/lifecycle.ex:158: anonymous fn/4 in Phoenix.LiveView.Lifecycle.mount/3
(phoenix_live_view 1.0.17) lib/phoenix_live_view/lifecycle.ex:237: Phoenix.LiveView.Lifecycle.reduce_socket/3
(phoenix_live_view 1.0.17) lib/phoenix_live_view/utils.ex:346: anonymous fn/6 in Phoenix.LiveView.Utils.maybe_call_live_view_mount!/5
But /auth/user/magic_link loads just fine. Any ideas where I could have made a mistake? Versions: Phoenix: 1.8.0-rc.3 Ash: 3.5.15 Ash authentication: 4.9.0 Ash authentication phoenix: 2.7.0
19 Replies
barnabasj
barnabasj•4mo ago
did you just install everything with igniter from the ash-hq page? just trying to understand the best way to reproduce
ggarciajr
ggarciajrOP•4mo ago
I created the new project as a regular phoenix project and than added ash using the commands provided in the ash-hq page, using the "already have an app" option this is a brand new app. so no customization done at this point
barnabasj
barnabasj•4mo ago
Ok, let me run the same than
ggarciajr
ggarciajrOP•4mo ago
ah, two things. I'm following the mulitenancy (context) and only enabled the magic link strategy not sure if that matters
barnabasj
barnabasj•4mo ago
is there an option for multitenancy in the installer that you used or does that mean you edited the resources to add the multitenancy config?
ggarciajr
ggarciajrOP•4mo ago
I added this to the resource
multitenancy do
strategy :context
end
multitenancy do
strategy :context
end
I followed this tutorial https://hexdocs.pm/ash/multitenancy.html#context-multitenancy
barnabasj
barnabasj•4mo ago
So far I'm unable to reproduce, have you changed anything else in the code? The error would suggest maybe something in the pipeline is setting the current_user before the default hook is run
ggarciajr
ggarciajrOP•4mo ago
this is the pipeline:
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session

plug :fetch_live_flash
plug :put_root_layout, html: {MyApp.Layouts, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
plug :load_from_session
end
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session

plug :fetch_live_flash
plug :put_root_layout, html: {MyApp.Layouts, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
plug :load_from_session
end
and the route
# Remove these if you'd like to use your own authentication views
sign_in_route register_path: "/register",
reset_path: "/reset",
auth_routes_prefix: "/auth",
on_mount: [{MyAppWeb.LiveUserAuth, :live_no_user}],
overrides: [
MyAppWeb.AuthOverrides,
AshAuthentication.Phoenix.Overrides.Default
]
# Remove these if you'd like to use your own authentication views
sign_in_route register_path: "/register",
reset_path: "/reset",
auth_routes_prefix: "/auth",
on_mount: [{MyAppWeb.LiveUserAuth, :live_no_user}],
overrides: [
MyAppWeb.AuthOverrides,
AshAuthentication.Phoenix.Overrides.Default
]
I can start over and diff the results to try to find the problem. Thanks for your assistance though
barnabasj
barnabasj•4mo ago
That looks pretty standard, if you find that after installing with igniter in a new project you get the same error, maybe you can push that code somewhere so we can clone it and look at it directly
ggarciajr
ggarciajrOP•4mo ago
sounds good. btw, did you add phoenix with igniter? fyi this is how I started the project mix phx.new myapp --module MyApp --no-dashboard --no-ecto
barnabasj
barnabasj•4mo ago
I did it this way mix phx.new myapp without the extra flags why did you use the no-ecto flag? I think AshPostgres will setup correctly in both cases. But I've only used it with Pheonix and Ecto before.
ggarciajr
ggarciajrOP•4mo ago
I thought I didn't need ecto config and what not after I installed ash, given ash would configure everything needed
barnabasj
barnabasj•4mo ago
you are probably right It's mostly the same setup though, we only put some extra things in the repo module IIRC
ggarciajr
ggarciajrOP•4mo ago
qq about the multi tenancy. when you use context, users are created directly in the tenant schema, rendering the users table in the public schema useless?
barnabasj
barnabasj•4mo ago
pretty much, in the applications I worked in the users were always global, because most of the time you want to use the user to then see which org they belong to.
Kapsy
Kapsy•4mo ago
I'm setting up 1.18 rc3 via the command generated on the webpage, will post if I get the same error.
ZachDaniel
ZachDaniel•4mo ago
The one on the webpage reinstalls the 1.17 installer 😅 We should make it detect a newer one and ask the user if they want to use the new one
Kapsy
Kapsy•4mo ago
Yeah, I just fetched the bash via curl and checked what it did and changed to 1.18. But it would probably be good to have some kind of information on the webpage on what version it fetches (maybe an option in the selector?)
ggarciajr
ggarciajrOP•4mo ago
@barnabasj just to give you an update, I couldn't reproduce the error either so far.

Did you find this page helpful?