tnederlof
Phoenix Auth Liveview Current User
I have been playing around with the new Phoenix Auth generator (https://github.com/aaronrenner/phx_gen_auth) and have been learning a lot just reading through the design/code of the files generated so thank you to anyone who worked on it, impressive! My question is what are the best practices for working with the current user in a liveview .ex file.
When using live view I often want to display a list of items that belong to a certain user. In my item context I have a function set up to talk to ecto and list out items that are associated with a certain user_id.
Items.list_user_items(user) for example.
In order to get the user to feed the function I have found this following code works in the mount call.
def mount(_params, session, socket) do
user = Accounts.get_user_by_session_token(session["user_token"])
{:ok, assign(socket, :items, Items.list_user_items(user))}
end
However in a function like handle_event that does not have the session, how would I properly pass the current user to the list_user_items function?
def handle_event("delete", %{"id" => id}, socket) do
item = Items.get_item!(id)
{:ok, _} = Items.delete_item(item)
{:noreply, assign(socket, :items, Items.list_user_items(**user**))}
end
Marked As Solved
idi527
Also Liked
tnederlof
Ha that might have been the quickest answer, kind of embarrassed that didn’t occur to me.
Thank you so much for that link, that page is EXACTLY what I need!








