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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
jdufour
Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New

Other popular topics Top

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
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
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1143 25883 760
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New

Sub Categories: