[Question] How do store user location in session when using LiveView?

just another Phoenix App with LiveView.

We have @ current_user in our session data.
Additionally, I want to store current_region of user…

lets say when users opens in browser my_domain_com a default current_region like abc must be set.
Instead if user visits my_domain_com / region/r_123 , I want r_123 to be stored in session data under current_region. (Assume QR code is scanned and it opens this link.)

Yes, I read LiveView cannot store/edit session data. Looking for simple hack if anyone has used. I dont understand the need of phx-hook here in this case.

This is one of nearest blogs I came across,

  scope "/", AtmanirbharWeb do
    pipe_through :browser

    live "/", PageLive, :index

The possible Idea clicks in my mind, instead of live make this a get request. Confused so posting the question here.

Still open to other ideas and implementations.

use a plug? or a controller for /region/r_123 that quickly sets cookie and redirects to /region

1 Like

I use a similar approach like the one described in the article you linked. The phx-hook will be present in a template used on your /region/r_123 route like and will fire an AJAX call to update current_region in the session.

A plug will only work on the first render in a LV scenario. Any further LV navigation (going from region to region in this case) won’t go through the plug, thus the session won’t be updated.

1 Like