Hasimbola

Hasimbola

Update Session inside liveview

Hello everybody, did someone know how to update session inside liveview?

Thanks! :smiley:

Most Liked

msimonborg

msimonborg

You have to make a HTTP request to a controller to mutate the session cookie. It is possible to do without navigating away from your live view, but YMMV and you’ll have to decide if this works for your use case.

In my case, I want to update certain user settings in the session. It is possible to target a form submission to a hidden iframe, so you can submit a form through the iframe without reloading your page. There’s a little extra setup, but you can use this with live view.

I place the iframe at the bottom of my live layout:

<!-- This iframe provides a target for hidden form submissions and HTTP requests -->
<!-- that mutate the session state. -->
<iframe hidden name="hidden_iframe" src={Routes.iframe_path(@socket, :index)} height="0" width="0">
</iframe>

It needs a src so I have an /iframe route with a simple controller action that just returns html(conn, "ok"). Of course this means it’s making an extra HTTP request to your server every time you load a page that contains this iframe. Then create your form targeting the hidden_iframe

<.form
  let={f}
  for={:settings}
  action={Routes.settings_path(@socket, :update)}
  method="put"
  target="hidden_iframe"
>

In my case, my SettingsController does as little as possible, it validates the input with an embedded changeset, updates the session, and returns html(conn, "ok") or html(conn, "error: #{errors}")

LostKobrakai

LostKobrakai

If it’s for preferences (non personal data) a separate cookie (non http-only) could be used with push_event to customize from JS.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement