connected-cjohnston

connected-cjohnston

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.

Showing Posts 1 to 7

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.

sodapopcan

sodapopcan

Does it work if you put it in the live.html.heex or app.html.heex layouts? I don’t believe that @current_user works in the root layout and I believe you generally want to keep dynamic stuff out of root. See here.

When I had both dead and liveviews, I used a component for displaying this stuff and called it in each layout.

linusdm

linusdm

That should work. It’s how phx.gen.auth adapts the root template.

sodapopcan

sodapopcan

Ah ok. From my memory it didn’t used to work so I always put stuff in the layouts. I’ve otherwise never had this problem myself so it’s odd!

connected-cjohnston

connected-cjohnston OP

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.

linusdm

linusdm

Maybe you modified the browser pipeline in the router in a way the generator was confused? I’m not sure how that works exactly, but should be easy to find in the phoenix sources. I’d expect a warning when the generator cannot inject the plug (the generators also warn when a file it wants to generate already exists). Good luck with the app!

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews