Chrichton

Chrichton

Programming Phoenix LiveView: B03 (page 56-57) auth exercise

Dear Sophie.
I tried to do the “Authorization” exercise and have two questions:

  1. When trying to plug in an email-service, I found the function “&Routes.user_confirmation_url(conn, :confirm, &1)” in “user_confirmation_controller.ex”.
    I wasn’t able to find this function.
    Where is it located or how is it created dynamically?

  2. I added the “username” field without problem, but when I set it to unique and made it safe with “unsafe_validate_unique(:username, Pento.Repo)” some tests failed. I tried to fix them via modifying the fixture()-Function, but still two test concerning the uniqueness of the “email” failed.
    Is there an easy way to get the generated tests running, when adding a second unique field?

Thanks for your superb book.
I learned a lot from this exercise, especially the sending of the confirmation-email via Bamboo and Sendgrid.

Best wishes from Heiko

Most Liked

SophieDeBenedetto

SophieDeBenedetto

Author of Programming Phoenix LiveView

Hi again @Chrichton!

So the Routes.user_confirmation_url/3 isn’t defined as such in your router. It’s defined under the hood of Phoenix by virtue of the user_confirmation_path routes that were added to your router.ex file by the Phoenix Auth generator. The function itself is passed as an argument to the Accounts.deliver_user_confirmation_instructions/3 function in the UserConfirmationController controller. In that Accounts function, the Routes.user_confirmation_url/3 is finally invoked to return the user confirmation URL, with the correct user token, to be rendered in the email sent to the user. I would trace the code flow in the Accounts.deliver_user_confirmation_instructions/3 to get a better understanding of how the Routes.user_confirmation_url/3 function is being used.

Re: your exercise solution:

It looks like your solution will ensure that a user is redirected to the "/guess" route when the complete the log in process. But the challenge is actually asking you to ensure that if a user who is logged in visits /, that they get automatically redirected to /guess. You can solve this a few different ways.

  • Add a function plug to the :browser pipeline in the router.ex to redirect the user from / to /guess if they are logged in. Like this:
#router.ex
defmodule PentoWeb.Router do
  use PentoWeb, :router
  import Phoenix.Controller
  ...
  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, {PentoWeb.LayoutView, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug :fetch_current_user
    plug :redirect_if_logged_in # add me!
  end
  
  ...
  
  def redirect_if_logged_in(conn, _opts) do
    if conn.assigns.current_user && conn.request_path == "/" do
      redirect(conn, to: "/guess")
    end
    conn
  end
end

This requires importing the Phoenix.Controller module into the router to get the redirect/2 function, which I don’t love.

You could also add some logic to the PageLive live view’s mount function to do a redirect if there is a logged in user. This is the live view that handles the / route. It would look something like this:

# lib/pento_web/live/page_live.ex
 def mount(_params, %{"user_token" => _}, socket) do
    {:ok, redirect(socket, to: "/guess")}
  end

  def mount(_params, session, socket) do
    {:ok, assign(socket, query: "", results: %{})}
  end

Here, you are checking to see if there is a logged in user based on whether or not the session argument given to mount has a "user_token"key present. If so, there is a logged in user and you can use theredirect` function to do a redirect.

Popular Prag Prog topics Top

kuroneko
Whilst the author has been careful to provide exact results for the tests elsewhere in the book (such as surds with the transformation te...
New
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
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
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
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex> product = %Product{} %Pento....
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
brunogirin
When running tox for the first time, I got the following error: ERROR: InterpreterNotFound: python3.10 I realised that I was running ...
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New

Other popular topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
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
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New
New

Latest in PragProg

View all threads ❯