
Sanjibukai
Programming Phoenix LiveView: current_user and assign_new (page 60)
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.
First Post!

flavius-popan
I’ve struggled to understand this exercise too, it’s really hard to wrap my head around and I wonder if the auth code has changed too much in v1.0.0 to still be a valid challenge.
Popular Pragmatic Bookshelf topics










Other popular topics










Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /onivim
- /svelte
- /typescript
- /crystal
- /c-plus-plus
- /kotlin
- /tailwind
- /gleam
- /ocaml
- /react
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /zig
- /scala
- /textmate
- /sublime-text
- /nixos
- /debian
- /lisp
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /ubuntu
- /spring
- /manjaro
- /nodejs
- /diversity
- /lua
- /c
- /julia
- /slackware
- /markdown