martin.jahn

martin.jahn

Current user in Phoenix LiveView

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.

Marked As Solved

martin.jahn

martin.jahn

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 %>

Also Liked

acrolink

acrolink

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

Section: Override action/2 in your controllers

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.

c4710n

c4710n

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

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement