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

sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=> (create-...
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
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |> handle_event() |> render() but the correc...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
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

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
New

Sub Categories: