Hello everyone,
In page 60, the book is suggesting the following about reducing the number of database call when fetching the user in LiveView on_mount
:
This more advanced problem gives you a chance to optimize your LiveView
authorization code.In the PentoWeb.UserAuthLive.on_mount/4 callback, assign the socket assigns
key of :current_user using the assign_new/38 function in order to ensure that
you don’t need to make additional database calls:
- When the live view first mounts in its disconnected state and the plug
pipeline has already populated :current_user in the Plug.Conn struct.- If the live view is being redirected to itself, and its socket assigns
already contains a key of :current_user.
So I guess the solution is to do the following:
def on_mount(_, _params, %{"user_token" => user_token} = session, socket) do
socket =
socket
|> assign_new(:current_user, fn -> Accounts.get_user_by_session_token(user_token) end)
if socket.assigns.current_user do
{:cont, socket}
else
{:halt, redirect(socket, to: "/users/log_in")}
end
end
Doing so reduce by one the database call indeed. But I noticed that’s there is still 2 database calls…
One from that on_mount
and one from the earlier plug fetch_current_user
.
I guess the reason it’s because in the plug we are populating the current_user
in the Plug.Conn
while only leaving the user_token
in the session.
Then in the LiveView mount since we are not reading from the Conn but from the session and because there is no current_user
in the session but only the user_token
, that second database call is somehow mandatory…
But as suggested in the exercise (the first bullet point above), how can we leverage the fact that the current_user
already exists in the Plug.Conn
so that there is now really only one user fetching in the databse?
Thank you very much.