I generated two sets of scaffolds: one for phx.gen.auth, and one for phx.gen.live for a model called Article
. I am using Phoenix 1.7.0. I am trying to access the current_user
inside of my live_component
(which was generated by phx.gen.live
). In particular, I have a reference field on my Article model that I am trying to assign to the current_user
in new
and edit
actions in my form_component.ex
file. How can I accomplish this?
When you create the user you can add that to socket.assigns. A live component is on the same process as the live view so you should be able to access the state.
Where exactly do I add it to the assigns? I tried assigning it in the mount
function of index.ex
, but I still can’t seem to access it in the form_component.ex
actions. Do I need to assign it in every endpoint along the way as well?
Live components have a callback mount(socket)
OK, figured it out. Thanks for your help!
Happy to help!