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.

Popular Prag Prog topics Top

telemachus
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks” While running the smoke tests in Chapter 2, I get these...
New
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
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
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
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
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

Other popular topics Top

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
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
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
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
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
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
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
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
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
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New

Latest in PragProg

View all threads ❯