Interact with session from within live view

Hey all!

I want to save some sort of user settings into the session (cookie) from within a LiveView. I’ve created a form that submits the corresponding data to a Phoenix.Controller.

The form in the LiveView looks like this:

<.form :let={f} for={:data} action="/controller_route">
  ...
</.form> 

The controller looks like this:

def update(conn, %{} = params) do
   conn
   |> put_session(:test, "hello world")
   |> send_resp(200, "Session updated")
end

Is there a way to prevent the LiveView from disconnecting when submitting the form? The controller should save the data in the session, but I do not want to get redirected. I’ve created the controller to interact with the session only.

Is there another way to save data into the session from within a LiveView without getting redirected?

It is not an option to save the data in local storage, because then I will not have access to it on initial load.

Thank you!

1 Like

I’ve seen people use iframes to connect to controller based urls without dropping the LV connection. Not sure if that helps for session stuff though.

Another option would be using JS.dispatch to do a XHR request to update the session.

3 Likes