Asking for a bit code review with "your turn" section (Programming Phoenix LiveView)

Hi all!, id like to ask some help (just a minute or two would be super helpful) regarding with the book Programming Phoenix LiveView B6, Part I Chapter 2 “Your turn” section (page 60):

Give It a Try
These problems deal with small tweaks to the existing generated code.
  • If you already have an email service, try plugging it in to the generated authentication service so that it will really send the user an email when they register for an account. Did you have to add additional arguments to the existing functions?
  • Add a migration and a field to give the User schema a username field, and display that username instead of the email address when a user logs in. Did you require the username to be unique?
  • If a logged in user visits the / route, make them redirect to the /guess route. 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.

I made tweaks from that exercise and I’m super grateful for anyone who’d like to chime in for any nitpicks/code review.

Files that are affected:

  • priv/repo/migrations/20220307092917_add_username_field.exs.
  • config/config.exs lines 28-32 commented, added finch as api client (at the moment i’m using the local adapter).
  • lib/pento_web/controllers/page_controller.ex, redirects logged in user when visiting / route.
  • lib/pento_web/live/user_auth_live.ex line 8, from assign to assign_new.
  • lib/pento_web/templates/layout/_user_menu.html.heex line 3, displays username instead of email.

@AstonJ am I allowed to post like this with screenshots taken from the book?

2 Likes

It’s usually best not to post screen grabs of copyrighted material, so in this case I would suggest posting a thread in the #community:pragprog-customers section with your questions and hopefully somebody reading the book will be able to comment :023:

2 Likes

I updated it to #community:pragprog-costumers :pray:

1 Like