vfsoraki

vfsoraki

In mount/3 callback of a LiveView module, you get session as the second parameter. It works fine, until you use live_redirect/push_redirect to mount another LiveView.

In this case, what is the session passed to second one? The same as original?

Is there a way to change it, without some hacky thing?

I currently did it by using JS to append some signed query string to my url and reload the page, and a plug that reads that signed string and sets appropriate session values, but this feels kind of hacky.

My current use case is user settings page. User changes some settings, then hits save, then goes to another page. User settings is saved in database, but session data is still old, so when new LiveViews are mounted they still see the old setting.

Showing Posts 1 to 10

wanton7

wanton7

I might be wrong here because of my limited usage of LiveView. But if session is same as Phoenix session my understanding is that default implementation uses a cookie to save session values. Because LiveView uses Websocket it can’t create a cookie for browser. You likely have to look for alternative to session cookie implementation.

vfsoraki

vfsoraki OP

That’s something I failed to mention, though I don’t think will matter.

I’m not using cookies for session storage. I’m using a custom implementation that uses database for session storage. It’s an implementation for Plug.Session, so I don’t think it should matter what is my session storage.

wanton7

wanton7

How do match session to database values? I mean you probably still have a cookie that has a session id right? Maybe that cookie isn’t created?

vfsoraki

vfsoraki OP

I do have sessions working fine in normal HTTP requests. It works fine, and my session store also works fine. LiveView also gets the first session correctly.

Yes, only session id is saved in a cookie, and session data is stored in database.

I just implemented a store using Plug.Session.Store — Plug v1.20.2. Thats all, nothing tricky or fancy.

Maybe I should’ve reworded my question to something like “How to modify session when using LiveView, without refreshing the page?”

derek-zhou

derek-zhou

Presumably the session id will not change because the user changing their settings. You can just load whatever stuff from the database at the mount time of the liveview, right?

vfsoraki

vfsoraki OP

No, session id will not change when it’s modified.

Consider session has user key which has the data for current user. I can read this using def mount(_, %{"user" => user}, socket) in LiveView, but when I change user in anyway, there is no way to update session, presumably because there is no conn.

EDIT: I mean without a full refresh of course, like what live_redirect/push_redirect does.

Although one might say that session id is stored in a cookie, and that can be read by LiveView and it can also be modified then, but I think there is no way to do it right now.

I can reload my user in every LiveView like:

def mount(_, %{"user" => %{id: user_id}}, socket) do
  {:ok, assign(socket, user: Users.get!(user_id))}
end

But I think there may be a better way.

derek-zhou

derek-zhou

What do you mean by a better way? In plain HTTP you read it every time in your plug. In live_redirect your plug did not run so you would need to read it yourself in mount.

vfsoraki

vfsoraki OP

I know, so there is no way to update session before live_redirect.

You either should refresh data in your mount yourself (using id or whatever) or do a full refresh for plugs to run.

wanton7

wanton7

Googled a bit about this issue and came across this thread that might help you How to manage session state with live view

vfsoraki

vfsoraki OP

Nice solutions were provided at the end of that topic, but they seem kind of hacky as my solution. Thanks!

— 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
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
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
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
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews