1

I save "current_user" to the session variable in a plug. And I have the standard, auto-generated form-component or modal of LiveView. I'm using a modal in a part of a website where only authenticated users are allowed.

router:


    live_session :on_authenticated, on_mount: MyWebsiteWeb123.InitLiveassignss do
      scope "/", MyWebsiteWeb123 do
        pipe_through :browser

        live "/abc", MyLive
      end
    end

init assignss module:


    defmodule MyWebsiteWeb123.InitLiveassignss do
      import Phoenix.LiveView

      def on_mount(:on_authenticated, _params, session, socket) do
        cu = Repo.get!(User, session["current_user_id"])
        socket2 = assigns(socket, :current_user, cu)

        {:cont, socket2}
      end
    end

This nicely allows me to get access to the current_user in socket in the situations when "submit" button is clicked, in a normal form on a page.

However, not in a modal. That is, not when "save" button is clicked in it -- 'current_user' gets dissapered.


    //current user is nil
    def handle_event("save", %{"my_model" => my_model_params}, socket) do
      // 'current_user' doesn't exist anywhere in 'socket'
      a1 = socket.assigns[:current_user]

      //
      //a2 = socket.assigns[:current_user_id]
      //a3 = socket.assigns["current_user"]
      //a4 = socket.assigns["current_user_id"]
      //a5 = socket["current_user_id"]
      //a6 = ...........

      //......
    end

Where has current_user has disappered from assigns in a modal?

How to make all this work?

tutushka
  • 27
  • 6
  • 1
    Have you checked if you're matching the on_mount with inspect or a fallback/default function? – Hedde van der Heide Nov 03 '22 at 20:53
  • Is this modal just HTML in the live view? If not, and it's a live component, be sure to use preload or update to pass on the assign from live view to the live component. – thelastinuit Nov 05 '22 at 03:43
  • 1
    @thelastinuit it's live component. Right. But why is it that it's not described anywhere? Everywhere it says that I simply am able to get access to assigns directly -- from a live view, in a live component. Namely, I don't have to pass anything to live component additionaly – tutushka Nov 05 '22 at 03:52
  • Yeah, It is like implicit. When you call a component you can do like so: <.live_component module={MODULE} {assigns} />. It means it's passing ALL assigns (which btw it's not recommended). So, you can call it as <.live_component module={MODULE} current_user={assigns.current_user} />. That means, ONLY current_user will be passed on to the live component. assigns.current_user, that assigns it's from the live view where you are mounting the live component. – thelastinuit Nov 05 '22 at 03:59

1 Answers1

0

So since it's a live component. Follow this. Namely:

<.live_component module={YourLiveComponent} id="live-component-id" current_user={@current_user} />

you can call @current_user because HEEX's assigns from live view is defined by your on_mount.

thelastinuit
  • 311
  • 1
  • 5
  • 9
  • 1
    I've followed that. Where in that is it described? – tutushka Nov 05 '22 at 12:43
  • Right before the section Life-cycle: "You must always pass the module and id attributes. The id will be available as an assign and it must be used to uniquely identify the component. All other attributes will be available as assigns inside the LiveComponent." – thelastinuit Nov 05 '22 at 17:22
  • All the assigns become @ by HEEX sigil, btw. in your case live view at render function. those assigns are passed on to the html template where you probably are loading the live component. you can access assigns or their respective name with @. whatever you pass on to live component will be accesible at the preload or update function of the live component. – thelastinuit Nov 05 '22 at 17:24