martin.jahn

martin.jahn OP

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.

First 6 of 6 Posts Switch mode

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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement