# Programming Phoenix LiveView: B6.0 pg 56 - current-user not set

**URL:** <https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052>\
**Category:** PragProg Customers\
**Tags:** book-programming-phoenix-liveview, errata\
**Created:** [10 February 2022 19:44 UTC](https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052 "2022-02-10T19:44:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![taguniversalmachine](https://forum.devtalk.com/user_avatar/forum.devtalk.com/taguniversalmachine/32/3274_2.png) [@taguniversalmachine](https://forum.devtalk.com/u/taguniversalmachine)\
**Post date:** [10 February 2022 19:44 UTC](https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052/1 "2022-02-10T19:44:23Z")

</div>

It seems the second code snippet is missing the code to set the current\_user:

```auto
current_user: Accounts.get_user_by_session_token(session["user_token"]),

```

and on the previous page (55), the last sentence repeats the word “also”

it is also also protected

---

<div class="post-metadata">

**Author:** ![SophieDeBenedetto](https://forum.devtalk.com/user_avatar/forum.devtalk.com/sophiedebenedetto/32/2521_2.png) [@SophieDeBenedetto](https://forum.devtalk.com/u/SophieDeBenedetto)\
**Post date:** [10 February 2022 20:42 UTC](https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052/2 "2022-02-10T20:42:16Z")

</div>

You are correct! Check out the corrected code here [Programming Phoenix LiveView: Session Data B6.0 (p. 56)](https://forum.devtalk.com/t/programming-phoenix-liveview-session-data-b6-0-p-56/39101)

---

<div class="post-metadata">

**Author:** ![taguniversalmachine](https://forum.devtalk.com/user_avatar/forum.devtalk.com/taguniversalmachine/32/3274_2.png) [@taguniversalmachine](https://forum.devtalk.com/u/taguniversalmachine)\
**Post date:** [11 February 2022 19:22 UTC](https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052/3 "2022-02-11T19:22:18Z")

</div>

ok thanks, and while we are on the subject, I am having a problem with the live\_session work flow not going the way the book says. Im on Phoenix 1.6.0, and if I implement AuthLive the way the book has it, I first get an exception that UserAuthLive doesn’t implement mount/3

Request: GET /guess  
\*\* (exit) an exception was raised:  
\*\* (UndefinedFunctionError) function PentoWeb.UserAuthLive.mount/3 is undefined or private

I tried adding a trivial mount function

```auto
def mount(_, _, socket) do
      {:cont, socket}
  end

```

which clears the error, but on\_mount is never called, so current\_user never gets put into the assigns. I thought maybe I would try grabbing the data in mount since that one _is_ being called, but it is being called with empty maps on &1 and &2.

My route is just like in the book:

```auto
live_session :default, on_mount: PentoWeb.UserAuthLive do
    live "/guess", PentoWeb.WrongLive
  end

```

Any ideas about what I might be doing wrong? I realize this is not necessarily the forum to ask for debugging help but I am doing exactly what is in the book and figured I would ask here first in case it’s something simple.

---

<div class="post-metadata">

**Author:** ![SophieDeBenedetto](https://forum.devtalk.com/user_avatar/forum.devtalk.com/sophiedebenedetto/32/2521_2.png) [@SophieDeBenedetto](https://forum.devtalk.com/u/SophieDeBenedetto)\
**Post date:** [11 February 2022 20:30 UTC](https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052/4 "2022-02-11T20:30:31Z")

</div>

Hmm interesting. I’m not having that same issue. I’m running Phoenix 1.6.2 and LiveView 0.17.5.

Here’s my router:

```elixir
scope "/", PentoWeb do
    pipe_through [:browser, :require_authenticated_user]

    live_session :default, on_mount: PentoWeb.UserAuthLive do
      live "/guess", WrongLive
    end
#...

```

and my `UserAuthLive` module:

```elixir
defmodule PentoWeb.UserAuthLive do
  import Phoenix.LiveView
  alias Pento.Accounts

  def on_mount(_, params, %{"user_token" => user_token} = _session, socket) do
    socket =
      socket
      |> assign(:current_user, Accounts.get_user_by_session_token(user_token))
    if socket.assigns.current_user do
      {:cont, socket}
    else
      {:halt, redirect(socket, to: "/login")}
    end
  end
end

```

---

<div class="post-metadata">

**Author:** ![taguniversalmachine](https://forum.devtalk.com/user_avatar/forum.devtalk.com/taguniversalmachine/32/3274_2.png) [@taguniversalmachine](https://forum.devtalk.com/u/taguniversalmachine)\
**Post date:** [16 February 2022 19:39 UTC](https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052/5 "2022-02-16T19:39:01Z")

</div>

Ok I have it working now but not really sure if I did it right. First I had to update live view to 0.17.0, because according to the [docs](https://hexdocs.pm/phoenix_live_view/security-model.html) this feature was only added in 017.0, which I think is odd because surely the book was written against an older version. Then I used the on\_mount directive in WrongLive:

```auto
defmodule PentoWeb.WrongLive do
  use Phoenix.LiveView
  alias PentoWeb.Router.Helpers, as: Routes
  alias Pento.Accounts
  on_mount PentoWeb.UserAuthLive

```

and modify the signature of on\_mount in UserAuthLive according to the docs:

```auto
def on_mount(:default, _, %{"user_token" => user_token} = session, socket) do

```

That got on\_mount being called, but it was getting called without any data:

> Request: GET /guess  
> \*\* (exit) an exception was raised:  
> \*\* (FunctionClauseError) no function clause matching in PentoWeb.UserAuthLive.on\_mount/4  
> (pento 0.1.0) lib/pento\_web/live/user\_auth\_live.ex:5: PentoWeb.UserAuthLive.on\_mount(:default, %{}, %{}, #phoenix.LiveView.Socket\<assigns: %{ **changed** : %{}, flash: %{}, live\_action: nil}, endpoint: PentoWeb.Endpoint, id: “phx-FtRahGg3HTi5cqYk”, parent\_pid: nil, root\_pid: nil, router: PentoWeb.Router, transport\_pid: nil, view: PentoWeb.WrongLive, …\>)

I then removed the live\_session declaration from router.ex:

```auto
 # live_session :default, on_mount: PentoWeb.UserAuthLive do
 # live "/guess", PentoWeb.WrongLive
 # end

```

and added back the original route:

```auto
scope "/", PentoWeb do
    pipe_through [:browser, :require_authenticated_user]

    live "/guess", WrongLive
    get "/users/settings", UserSettingsController, :edit
    put "/users/settings", UserSettingsController, :update
    get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email
  end

```

And now it works, I am getting the current\_user through the whole flow.

---

<div class="post-metadata">

**Author:** ![sampalest](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@sampalest](https://forum.devtalk.com/u/sampalest)\
**Post date:** [23 February 2022 18:42 UTC](https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052/6 "2022-02-23T18:42:25Z")

</div>

Hi,  
I have the same problem and I solved it updating the version of live\_view.  
In your mix.exs you have to set another version. (I used the same version of [SophieDeBenedetto])

```auto
{:phoenix_live_view, "~> 0.17.5", override: true},

```

Then, you have to execute this in your project

```auto
mix deps.get

```

I hope it helps

---

<div class="post-metadata">

**Author:** ![kondera](https://forum.devtalk.com/user_avatar/forum.devtalk.com/kondera/32/4690_2.png) [@kondera](https://forum.devtalk.com/u/kondera)\
**Post date:** [14 December 2022 17:55 UTC](https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052/7 "2022-12-14T17:55:01Z")

</div>

I had the same problem. At the top of Router I had alias PentoWeb.WrongLive that was cause of the problem.

---

<div class="post-metadata">

**Author:** ![kevinpostlewaite](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@kevinpostlewaite](https://forum.devtalk.com/u/kevinpostlewaite)\
**Post date:** [8 January 2023 02:58 UTC](https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052/8 "2023-01-08T02:58:00Z")

</div>

For the code addition to be made to file auth/pento/lib/pento\_web/router.ex on p58 (by page numbering) change:  
“Add this live\_session definition in the router within the scope “/”, PentoWeb scope definition:”

to:  
“Add this live\_session definition in the router within the scope “/”, PentoWeb scope definition above where you specify the actual route (live “/guess”, WrongLive):”

or similar wording, or include the route in the code snippet. Otherwise, the map will not be updated prior to hitting the user’s function, which may not be obvious to beginners 🙂

---

<div class="post-metadata">

**Author:** ![kevinpostlewaite](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@kevinpostlewaite](https://forum.devtalk.com/u/kevinpostlewaite)\
**Post date:** [11 January 2023 03:06 UTC](https://forum.devtalk.com/t/programming-phoenix-liveview-b6-0-pg-56-current-user-not-set/40052/9 "2023-01-11T03:06:26Z")

</div>

For the code, pp 79-80:  
generators/pento/priv/repo/seeds.exs

When you copy/paste the code example from the PDF you miss the part:  
name: “Table Tennis”,  
description: “Bat the ball back and forth. Don’t miss!”, sku: 15\_222\_324,  
unit\_price: 12.00

because it somehow falls at end of the page, outside of the rest of the code entry.
