connected-cjohnston

connected-cjohnston

Current_user not found error after running phx.gen.auth

I have encountered this error on two different code bases in the last few days. After running phx.gen.auth and following the instructions (e.g., mix deps.get and mix ecto.migrate) and running the server, I get the following error

key :current_user not found in: %{conn: %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{flash: %{}, ...

Both code bases were fairly small and neither had any form of User or authentication. One code base I ran phx.gen.auth for regular controllers and the other was for LiveView. Both had the above error. Every tutorial and guide I have watched has shown this to “just work”.

The problem seems to lie with the following code snippet in root.html.heex

  <body>
    <ul class="relative z-10 flex items-center gap-4 px-4 sm:px-6 lg:px-8 justify-end">
      <%= if @current_user do %>
        <li class="text-[0.8125rem] leading-6 text-zinc-900">
          <%= @current_user.email %>
        </li>
        <li>

Phoenix version: 1.7.2
Elixir version: 1.14.5
Erlang version: 25

One of the code bases I ran phx.gen.auth on was from the pragstudio LiveView course.

Marked As Solved

linusdm

linusdm

Do you have the :fetch_current_user plug in place, in your router pipeline somewhere? That’s the plug that puts the current_user in the right place, to be able to pick it up later. The plug is generated and implemented in your MyAppWeb.UserAuth module.

Your @current_user assign can be nil, if there is no logged in user. But you must have the plug in place to at least have the assign available. At least, if you wish to use the root template as modified by phx.gen.auth.

Also Liked

Aadmaa

Aadmaa

@linusdm’s answer solved my similar issue. FWIW: if ElixirLS has reformatted the router.ex file, you might find that fetch_current_user cannot get added to router.ex by the generator. In a router.ex with the generated structure, this goes at the end of the “pipeline :browser do” block:

# Inside router.ex 

pipeline :browser do
    plug(:accepts, ["html"])
    plug(:fetch_session)
    plug(:fetch_live_flash)
    plug(:put_root_layout, html: {LiveViewStudioWeb.Layouts, :root})
    plug(:protect_from_forgery)
    plug(:put_secure_browser_headers)
    plug :fetch_current_user # <--- add this one if missing!
  end

connected-cjohnston

connected-cjohnston

Yes, that was the problem. The plug was missing from the router. Thank you @linusdm

Odd that phx.gen.auth didn’t inject that code into the router pipeline for me. As I said, it just seems to work for everyone else.

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement