mitkins
Get User Id with Pow from session for Live View?
I’m in the process of switching from Guardian to Pow. Now I’m trying to figure out how to get the current user in my Live View. I’ve figured out what my :session_key is - so I’m passing that via the live view route:
live "/tasks", TaskLive.Index, session: [ :zoinks_auth ], as: :task_live
And here’s what I want to do in my mount:
def mount( session, socket) do
socket = assign_new( socket, :current_user, fn -> Pow.Plug.current_user(session.zoinks_auth) end )
{:ok, socket}
end
Though current_user expects a conn. Is there a way to retrieve the current user in Pow with just the :session_key (and no Connection)?
Most Liked
danschultzer
I’m going to set up a website with LiveView very soon, and will look into this then.
One caveat with LiveView is that the session won’t be reloaded or renewed like with the REST endpoints. It may make sense to get the socket to keep the session alive, but not sure if session can be updated through LiveView.
Pow.Plug.current_user/1 pulls the :current_user assigns from the conn, after the conn has been through the Pow.Plug.Session plug. It’s required to pass a conn.
In the LiveView docs, the user id is passed in the session and then loading it in mount/2. So you could set up a plug in your pipeline that updates a session value with the current user id, and then load from the DB in mount/2.
You can also pull the credentials directly by using the :zoinks_auth value. You have to call Pow.Store.CredentialsCache.get([backend: Pow.Store.Backend.EtsCache], session_key), but be warned that you’ll be working with the internals of Pow. It’ll return a tuple with the user being the first element {user, _ inserted_at}, and a :not_found atom is returned if there is no user stored.
I’ll have to work with LiveView to see what can be the best approach to deal with short lived sessions, and retrieving session data.
If anybody has ideas for how this could work, please do reply as I’m just scratching the surface of LiveView ![]()
LostKobrakai
I’ve build a small helper module, which at an interval checks if the user is still authenticated. There are a few hardcoded assumptions in there, but might be helpful:
e-nikolov
I’m pretty new to elixir. Can you please show an example of the correct syntax for the MFA?
Edit:
To answer my own question, it looks like this works:
live "/test", TestWeb.TestLive, session: {__MODULE__, :with_session, []}
def with_session(conn) do
%{"current_user" => conn.assigns.current_user.id}
end
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









