To me, there seems to be no difference, but I would like to confirm if I’m correct. If I am, please let me know if there are any considerations I should be aware of in your opinion. Could you please take a look? Thank you.
The first group contains a Plugs pipeline inside the live_session for the admin, while the second group has the Plugs pipeline outside the live_session but scoped to it. Here are the two code examples for reference:
First group:
live_session :admin, on_mount: MyAppWeb.AdminLiveAuth do
scope "/" do
# Plugs pipeline inside live_session
pipe_through [MyAppWeb.AdminPlugAuth]
# Live routes
live "/admin", AdminDashboardLive, :index
live "/admin/posts", AdminPostLive, :index
end
end
Second group:
scope "/" do
# Plugs pipeline outside live_session but scoped to it
pipe_through [MyAppWeb.AdminPlugAuth]
live_session :admin, on_mount: MyAppWeb.AdminLiveAuth do
# Live routes
live "/admin", AdminDashboardLive, :index
live "/admin/posts", AdminPostLive, :index
end
end
You’re right, both code snippets achieve the same outcome: the MyAppWeb.AdminPlugAuth pipeline will be applied to the /admin and /admin/posts live routes. The main difference is code organization. The first group keeps the plug pipeline inside the live_session, which might be more readable for associating authentication with the session. The second group places it outside but scoped to the session, offering slightly more flexibility if you later need routes within the same scope without admin authentication. The order of execution is slightly different but unlikely to have practical consequences in most scenarios. Choose the style you find more maintainable.