tnederlof

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

idi527

:wave:

You’d probably store the current_user in assigns in mount so that it’s accessible at socket.assigns.current_user in other callbacks like handle_event.

You can also create a helper like assign_mount in which you’d assign the user to the socket if you need it in more than one liveivew.

Also Liked

tnederlof

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!

unsek

unsek

For those who can’t find the page linked by @idi527, it got moved to its own page.

aegatlin

aegatlin

And here’s that same link without the version specified (so it defaults to “latest”)

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41454 115
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New

We're in Beta

About us Mission Statement