How to access session data in handle_params of LiveView?

Here’s a part of my liveview:

defmodule App1Web.SiteLive.Index do
  use App1Web, :live_view

  @impl true
  def mount(_params, session, socket) do
    user_id = session["current_user_id"]

    # .......
    # .......

    {:ok, assign(socket, :my_items, my_items)}

How will I also access user_id in this and analogous functions of the same module:

@impl true
def handle_event("delete", %{"id" => id}, socket) do
  # .........
  # how to access session["current_user_id"]   ????


Or should I have saved user_id somewhere in mount?

If you want something from the session, then yes. mount comes before handle_params in the lifecycle and then events can be handled with handle_event.


  defp mounted(_params, %{"user_id"=>user_id} = _session, socket), do:
     |> assign(
       user_id: user_id

   def handle_params(_params, _uri, %{assigns: %{user_id: user_id}} = socket) do