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.

Where Next?

Popular Pragmatic Bookshelf topics Top

yulkin
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=> (create-...
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
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
JohnS
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. With: ruby-3.0.0 s...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
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
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New

Other popular topics Top

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
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
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
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
New
New

Sub Categories: