mitkins
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)?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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/1pulls the:current_userassigns from the conn, after the conn has been through thePow.Plug.Sessionplug. 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 inmount/2.You can also pull the credentials directly by using the
:zoinks_authvalue. You have to callPow.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_foundatom 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:
danschultzer
This is great, thanks @LostKobrakai! I’ve refactored the code here to make it work in most cases: Instructions for WebSocket usage (e.g. Phoenix Channels and LiveView) · Issue #271 · pow-auth/pow · GitHub
And some thoughts on keeping session alive at the end:
kreiling.io
FYI: Phoenix LiveView 0.9.0 allows you to pass a function for the
sessionoption oflive/4andlive_render/3.Relevant docs: Phoenix.LiveView.Router — Phoenix LiveView v1.2.5
The docs don’t quite make it clear, but the
argslist passed in the MFA has thePlug.Connstruct (associated with the current request) prepended to it. The function should return a map with String keys, which is passed as thesession(second) arg to themount/3function.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:
OvermindDL1
MFA is always
{Module, FunctionName, ArgList}. So to callMyModule.blah()then it would be{MyModule, :blah, []}, or to callSomething.vreep(1, 2, 3)then it would be{Something, :vreep, [1, 2, 3]}.Now when a callback is done with extra arguments, they are traditionally always prepended (since that’s the fast operation), so when the
connis prepended then to call something likeModule.blah(conn, 1, 2)you’d encode it as{Module, :blah, [1, 2]}and the caller will prepend theconn.timc
Is there a clearer example as I am also having trouble with using a session with a live view.
In the test setup I authenticate as per my other tests, but in the actual test I have this so far:
and just taken from above:
Any help would be appreciated.
tenzil
@danschultzer, any new features in POW for 1.5 phoenix version, especially on the liveview part.
how to get current_user in Phoenix.LiveView?, registering users in liveview.
glennr
@tenzil check out this discussion happening over at the Pow GH repo: Instructions for WebSocket usage (e.g. Phoenix Channels and LiveView) · Issue #271 · pow-auth/pow · GitHub
unsek
Thank you! I was struggling with
:not_foundwhen callingPow.Store.CredentialsCache.getand the solution was in that issue.