LiveView - passing info from the conn to the socket

Hi,

What would be a good way to pass some information from the conn to the LiveView socket ?
Let’s assume the following example

router.ex

scope "/admin", MyAppWeb.Admin do
  ......
  pipe_through :ensure_admin_authenticated
  live "/article/:id/edit"
end

In my ensure_admin_authenticated plug I check the current user, see if it is admin and assign it to the conn.

I would like to have that available in the LiveView socket. For example, if I render the LiveView from a controller, I can use the session to pass the user, but I would not want to use controllers if possible.

Thanks

1 Like

You can a combination of assign_new and the session. Assuming your :ensure_admin_authenticated plug places a user_id key in the session, you can tell the live macro in the router to copy the conn session key to the LV session. Then on mount, you can use assign_new to first check the parent assigns for the :current_user, and fallback to lookup from the session. This allows the initial HTTP render to only fetch the user once via your plug. Then when the LV mounts in a connected state from the client, it will fallback to lookup the current_user from the session. For example:

scope "/admin", MyAppWeb.Admin do
  ......
  pipe_through :ensure_admin_authenticated
  live "/article/:id/edit", session: [:user_id]
end

...

def MyLive do

  def mount(%{user_id: user_id}, socket) do
    {:ok, assign_new(socket, :current_user, fn -> lookup_user(user_id) end)}
  end
end
13 Likes

Thanks Chris !

setting the user_id in the session worked perfectly.

For anybody following this subject, I ended up with a route like
live "/article/:article_id/edit", EditArticleLive, session: [:path_params, :current_user_id]

However, the assign_new only half worked for my case. In your example above it would always lookup_user/1 for me when the LV mounts.

If I inspect the conn, the first iteration would be

%Phoenix.LiveView.Socket{
  assigns: %{
    current_user: %MyApp.Accounts.Schema.User{
      __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
      id: 3,
      ...
    },
    my_text: ""
  },
  changed: %{current_user: true, my_text: true},
  connected?: false,
  endpoint: MyAppWeb.Endpoint,
  fingerprints: {nil, %{}},
  id: "phx-+acWjg8b",
  parent_pid: nil,
  private: %{
    assigned_new: {%{
       current_user: %MyApp.Accounts.Schema.User{
         __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
         id: 3,
         ...
       }
     }, [:current_user]}
  },
  stopped: nil
}

Which looks fine to me.
But then in the second iterations turns into this, if I do not fetch the User

%Phoenix.LiveView.Socket{
  assigns: %{current_user: nil, my_text: ""},
  changed: %{current_user: true, my_text: true},
  connected?: true,
  endpoint: MyAppWeb.Endpoint,
  fingerprints: {nil, %{}},
  id: "phx-noktpWPS",
  parent_pid: nil,
  private: %{assigned_new: {%{}, [:current_user]}},
  stopped: nil
}
3 Likes

hi @chrismccord , sorry non-LiveView question, how do i pass , session: [:user_id] to all routes inside specify scope

Hi! This does not seem possible anymore after this commit https://github.com/phoenixframework/phoenix_live_view/commit/0020c35a438fb9da0d26121cfe9da45e6813a486#diff-67a92e3716e8596fa160a63550ea8c6e Is there a alternative way of achieving the same?

8 Likes

ooh something I might be of help!

# at router.ex:
...
live "/", ThisIsLive #no need to do "session: ..."
...
put_session(conn, :session_id, session_id)

# at LiveView:
...
def mount(_params, session, socket) do
    session_id = session["session_id"]
   # session has whatever you pushed in router, via put_session()
...
end