Live View Session Management

Hi All

I have a project that I’ve returned to after a few months away. The project is using phoenix_live_session (GitHub - pentacent/phoenix_live_session: Live sessions for Phoenix LiveView and Phoenix Controllers) and find that it works really well.

For those folks that aren’t aware of the project - LiveSession provides in-memory sessions for LiveViews and Phoenix controllers.

I was just curious though, has liveview moved on such that a third party library is no longer required?

cheers

Dave

Hi @shortlyportly,

I was playing with live_session yesterday. I think it depends on what you want to use the session for but if it is to get information based on the session and populate the socket.assigns then the new live_session now has you covered.

You can create a Module that gets called using the on_mount hook (It works like Plug) and you can grab anything you need from the session and add it to the socket before mount is called. The main benefit is to reduce the boilerplate code on every LiveView.

Yesterday I moved the assign(socket, :current_user, user) to and on_mount Module and it works great. There is an example in the docs here

Andrew

Thanks Andrew.

My problem is actually the other way around in that I want to put something into the Session from a liveview. The point I need to do this is during a handle_params call at which point I’ve no longer got access to the Session.

cheers

Dave

Do you mind if I ask what you are putting in the session from a live_view?

Not at all :grinning:

My system allows users to manage multiple companies. Once they have logged on to the system they are presented with a “Select Company Screen”. This is a live_view. When they have selected a company I push this into the session using phoenix_live_session so that other live_views in the system know which company the user is working with.

If there is. better solution and I’m going about it wrong then I’m all ears.

cheers

Dave

You can push_event to the JavaScript side and stuff data in localStorage

Thanks @derek-zhou - I’d not thought of that. I’m also wondering whether I should just make my company selection a dead view so I get access to the session.

cheers

Dave

This, I believe the page could be a dead view, it would be simple and not feel hackey, just cuz we wanted to use liveviews across the whole app. unless theres a specific reason you want a liveview on that page.

Hi,

I think you are right. Not much benefit in having a live_view for company selection. It fits with the workflow of authentication and then the domain (i.e. company) for all subsequent actions.

I remember doing something similar but you could select the company from a profile dropdown and I would send the User back to a controller and change the domain and redirect back to the live_view. You have to reload the live_view for the new domain anyway.

Good luck.

Andrew

Thanks Andrew and Tom - a dead view it is.

cheers

Dave

Unless you want to enable the user to manage different companies on different devices at the same time, the easiest thing to do is still storing the choice on the server side in the database as a user preference.

Thanks for sharing your project! I have a live_session containing a few liveviews, all of which are controlled by a couple of parameters that the user can set on a top bar (present on all of those views). When I refreshed or live_redirected between those views, I would lose the assigns that were set for those parameters. I used your library to put those parameters into the session, and updated the session’s parameters on the callbacks for the top-bar’s form. Works like a charm! Now the parameters set in the top-bar are persisted across live_redirects and refreshes. In my opinion this is a great solution to the problem I was having. Do you think I could have solved this in a more elegant/appropriate way? Cheers, and thanks again.