Hi, when setting up ash_admin inside an authenticated session, I get
** (RuntimeError) attempting to define live_session :ash_admin inside :authentication_required.
live_session definitions cannot be nested. (phoenix_live_view 1.0.0-rc.6) lib/phoenix_live_view/router.ex:267: Phoenix.LiveView.Router.live_session/3
lib/db_backups_web/router.ex:27: (module)
what is the correct way of setting of ash admin in a protected route with ash authentication? Also, is there a way to override ash_admin core components?
hey in case anyone needs it, this is the way
ash_admin(“/admin”, on_mount: {YourAppWeb.LiveUserAuth, :live_user_required})
instead of
ash_authentication_live_session :authentication_required,
on_mount: {YourAppWeb.LiveUserAuth, :live_user_required} do
ash_admin("/admin")
end
which is what I was trying to do before
Oops, it doesnt actually work, now /admin just redirects to the login ALWAYS, if anyone knows how to do it, would be amazing
We need to invest some time/effort to make this integrate automatically, but this is how I’ve done it. It is a bit hacky but it works:
ash_authentication_live_session :admin_dashboard,
on_mount: [{WebuiltWeb.LiveUserAuth, :admins_only}], #<- notice this
session: {AshAdmin.Router, :__session__, [%{"prefix" => "/admin"}, []]},
root_layout: {AshAdmin.Layouts, :root} do
scope "/" do
pipe_through :browser
live "/admin/*route",
AshAdmin.PageLive,
:page,
private: %{
live_socket_path: "/live",
ash_admin_csp_nonce: %{
img: "ash_admin-Ed55GFnX",
style: "ash_admin-Ed55GFnX",
script: "ash_admin-Ed55GFnX"
}
}
end
end
Notice how I use a special clause to only allow admins to access the dashboard. If you want users who use the dashboard to act “as themselves” (and thus follow any policy rules with themselves as the actor), you’ll also want to specify an actor plug:
defmodule MyAppWeb.AshAdminActorPlug do
@moduledoc false
@behaviour AshAdmin.ActorPlug
@doc false
@impl true
def actor_assigns(socket, _session) do
dispatcher = socket.assigns[:current_user]
[actor: dispatcher]
end
@doc false
@impl true
def set_actor_session(conn), do: conn
end
and then configure it like so
config :ash_admin, :actor_plug, MyAppWeb.AshAdminActorPlug
We need to write a guide on this. This is relatively new/undocumented functionality still, but it is stable (i.e won’t change any time soon).