martin.jahn

martin.jahn

So I’m stuck here with session problems. I set current user to session in a plug because there is no other way to get data from conn to live view socket.

defp current_user(conn, _) do
  put_session(conn, :current_user, conn.assigns.current_user)
end

Than I assign it into socket in mount. So far so good. But it all falls apart the moment I change pages in live view. I have just phoenix generated live views. I was not able to access the user in def handle_event("save", %{"profile" => profile_params}, socket) no matter what I did. It simply vanishes somewhere along the way. I’m not able to figure out why. I tried everything I could to IO.inspect(socket) to find out where I loose this data and I was not able to figure it out.

Don’t worry I’ve already read “Pass in session data to a live view when instigated via a live route?”. But it’s not very useful in my case. I’ve already worked around the issue mentioned there.

Showing Posts 1 to 6

acrolink

acrolink

This can be helpful, but I have not tested it with liveview:

Section: Override action/2 in your controllers

sfusato

sfusato

Do you assign the :current_user in mount in all of your liveviews? Since you mention that it breaks when you change pages.

LostKobrakai

LostKobrakai

There’s a very good reason for this limitation: Data passed to liveviews will be serialized, put in the html and sent back by the client to start the liveview process via the websocket connection. Therefore you want to keep the data in there as minimal as possible. For authentication usually the user id is enough. In mount you can then fetch the actual user struct.

With assign_new you also have the option to pull already loaded data (e.g. by some plug) out of conn.assigns for the static render happening in the initial http request.

martin.jahn

martin.jahn OP

Sure I assign it to socket. assign(socket, profiles: fetch_profiles(), user_id: session["user_id"])
Basically when I click on this link I loose all data in assigns. <%= live_patch "New Profile", to: Routes.user_profile_index_path(@socket, :new) %>

This all happens when I used mix phx.gen.live. As I can see from docs assigns don’t change right after I change URL. Phoenix.LiveView — Phoenix LiveView v1.2.5

One more thing I noticed. I don’t have assigns from index.ex in form_component.ex. This is the problem I need to solve. Unfortunately it was not obvious at first glance.

EDIT:
I didn’t look at the index.html.leex. Because I thought code this important would not be hiding in templates. Anyway I need to define it here

  <%= live_modal @socket, RoyalWeb.User.ProfileLive.FormComponent,
+   user_id: @user_id, 
    id: @profile.id || :new,
    title: @page_title,
    action: @live_action,
    profile: @profile,
    return_to: Routes.user_profile_index_path(@socket, :index) %>
<% end %>
anuaralfetahe

anuaralfetahe

I had the exact same issue today. Same solution too. Had to pass the user_id thru the template.
Now I was wondering if this kind of data could be kept only on the back end side without passing to the client which he surely not need to know or edit. Would like to keep the user_id only attached to the beam process state on the backend site.

c4710n

c4710n

Up to 0.18.13, the doc of Phoenix LiveView has a dedicated section on this topic, you can check it out:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews