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.
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
linusdm
Do you have the
:fetch_current_userplug in place, in your router pipeline somewhere? That’s the plug that puts thecurrent_userin the right place, to be able to pick it up later. The plug is generated and implemented in yourMyAppWeb.UserAuthmodule.Your
@current_userassign can benil, 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 byphx.gen.auth.sodapopcan
Does it work if you put it in the
live.html.heexorapp.html.heexlayouts? I don’t believe that@current_userworks 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
That should work. It’s how
phx.gen.authadapts the root template.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
Yes, that was the problem. The plug was missing from the router. Thank you @linusdm
Odd that
phx.gen.authdidn’t inject that code into the router pipeline for me. As I said, it just seems to work for everyone else.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
@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: