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

:waving_hand:

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

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43657 311
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement