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
kszambelanczyk
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
RemyXRenard
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
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
samoloth
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
FlyingNoodle
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
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews