Sanjibukai

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

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.

Where Next?

Popular Pragmatic Bookshelf topics Top

yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
simonpeter
When I try the command to create a pair of migration files I get an error. user=> (create-migration "guestbook") Execution error (Ill...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
mcpierce
@mfazio23 I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
New

Other popular topics Top

AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
New
PragmaticBookshelf
Programming Ruby is the most complete book on Ruby, covering both the language itself and the standard library as well as commonly used t...
New
hilfordjames
There appears to have been an update that has changed the terminology for what has previously been known as the Taskbar Overflow - this h...
New

Sub Categories: