pentacent / phoenix_live_session Goto Github PK
View Code? Open in Web Editor NEWLive sessions for Phoenix LiveView and Phoenix Controllers
License: Apache License 2.0
Live sessions for Phoenix LiveView and Phoenix Controllers
License: Apache License 2.0
I'm very new to using Phoenix LiveView, so take this with a large grain of salt. I have Live Sessions working (super easy—thank you!), but they appear to not provide the current session when using push_redirect. The session available in the destination LiveView appears to be the default values until the page is refreshed to force a full request made which then loads the session values (including the "user_id" added from the previous page). Is there some other mechanism I should be utilizing when navigating across LiveViews to ensure the session is available?
Example of session from redirect:
%{
:__opts__ => [
clean_interval: 60000,
lifetime: 172800000,
table: :phoenix_live_sessions,
pub_sub: MyApp.PubSub,
signing_salt: "XXX"
],
:__sid__ => "XXX",
"_csrf_token" => "XXX"
}
And then as it's populated following the page reload:
%{
:__opts__ => [
clean_interval: 60000,
lifetime: 172800000,
table: :phoenix_live_sessions,
pub_sub: MyApp.PubSub,
signing_salt: "XXX"
],
:__sid__ => "XXX",
"_csrf_token" => "XXX",
"user_id" => 87632 // <--- the ID from the stored session from previous page
}
Hi
Me again :-). I have the following piece of code:
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :open, %{"id" => id}) do
socket
|> assign(:page_title, "Open Company")
|> assign(:company, Companies.get_company!(id))
PhoenixLiveSession.put_session(socket, "company_id", id)
end
As you know when a live view is first started it gets called twice, first as a regular http request and secondly as a socket. I "think" live_view session only gets set up once the socket is connected. If my :open acton is the first action the liveview needs to handle as it gets called as part of the initial http request. Because this is before the socket has been opened the call to put_session fails with the following error:
key :__live_session_id__ not found in: %{companies:.......
I believe the solution is to wrap PhoenixLiveSession.put_session in an if socket.connected? == true. That way there won't be an attempt to access the live_session_id key before it has been set up.
Does this makes sense or am I missing something.
BTW this is a fantastic library and exactly what I was looking for so thank you so much for writing it.
Okay so I have a very concrete and repeatable scenario where seemingly listeners aren't getting notified, where normally they do:
handle_info({:live_session_updated, _}, socket)
.However if I refresh the page I see the session has changed and also any future click events here on correctly notify others.
mount/3 calls local function put_session_assigns()
example code implements put_sesion_assigns()
:)
Hello.
Just following the instructions to use PhoenixLiveSession and I'm getting the error in the title key :key not found. It is unclear to me whether I should simply add the :key or if it is not required and I've done something wrong :-).
I'm using
{:phoenix, "~> 1.5.7"},
{:phoenix_live_view, "~> 0.15.0"},
{:phoenix_live_session, "~> 0.1"}
cheers
How does one use this library in a component? I got everything wired up, and tried to do this in my component:
def handle_event("browser-opened", params, socket) do
attrs = %{ browser_session: params["sessionId"] }
user = update_user(socket.assigns.current_user, attrs)
{
:noreply,
socket
|> PhoenixLiveSession.put_session("browser_opened", true)
|> assign(:browser_opened, true)
|> assign(:current_user, user)
}
end
Which resulted in:
(KeyError) key :__live_session_id__ not found in: (assigns contents)
I'm going to try and figure out what and how to pass down the live session id to the component and will post findings.
Looks like this is similar to #3.
Same feedback. Session management has been a huge problem for me when developing in live view, and this library solves it.
I'm not sure how to alleviate this issue, when I use live()
in my tests eventually down the chain the maybe_subscribe
fails due to a missing key.
Hi there
Thank you for your package, Phoenix_live_session, it is a great help.
I recently upgraded from v0.1.1 to v0.1.2 and now I get the following error when calling maybe_subscribe:
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in PhoenixLiveSession.maybe_subscribe/2
The call site is in the mount/3 function of my live component:
def mount(_params, session, socket) do
socket =
socket
|> maybe_subscribe(session)
IO.inspect session
{:ok, socket}
end
If I edit phoenix_live_session.ex in my deps folder and revert the changes to maybe_subscribe as follows, then it works again:
def maybe_subscribe(socket, session) do
if LiveView.connected?(socket) do
opts = session[:__opts__]
sid = session[:__sid__]
pub_sub = Keyword.fetch!(opts, :pub_sub)
channel = "live_session:#{sid}"
PubSub.subscribe(pub_sub, channel)
put_in(socket.private[:live_session], id: sid, opts: opts)
else
socket
end
end
The mount/3 call then succeeds, and produces the following output for 'session' via IO.inspect:
%{
:__opts__ => [
clean_interval: 60000,
lifetime: 172800000,
table: :phoenix_live_sessions,
pub_sub: XXXX.PubSub,
signing_salt: "XXXX"
],
:__sid__ => "XXXX",
"_csrf_token" => "XXXX"
}
Thanks in advance for any help you may provide. If you need any further info about the issue, please let me know..
I try to update some data within the session when a certain constellation has occurred. As soon as I use the function PhoenixLiveSession.put_session I get the following error:
Request: GET /dashboard
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in Keyword.fetch!/2
(elixir 1.12.3) lib/keyword.ex:417: Keyword.fetch!(nil, :table)
(phoenix_live_session 0.1.3) lib/phoenix_live_session.ex:159: PhoenixLiveSession.put_in/4
(phoenix_live_session 0.1.3) lib/phoenix_live_session.ex:261: PhoenixLiveSession.put_session/3
My mount looks like this:
@impl true
def mount(params, %{"locale" => locale} = session, socket) do
Gettext.put_locale(ExampleApp.Gettext, locale)
socket =
socket
|> PhoenixLiveSession.maybe_subscribe(session)
|> MountHelpers.assign_defaults(params, session, {@privileges, :read})
|> assign_breadcrumbs()
|> MountHelpers.assign_page_title()
{:ok, socket, temporary_assigns: [
user_groups: []
]
}
end
And i am using the handle info callback as described:
@impl true
def handle_info({:live_session_updated, session}, socket) do
{:noreply, MountHelpers.put_session_assigns(socket, session)}
end
My put session assigns function looks like this:
def put_session_assigns(socket, session) do
initial_tokens =
Map.take(session, ["__idx_token", "__idx_refresh_token", "__idx_id_token"])
tokens =
IdxClient.ensure_authenticated(:admin, initial_tokens)
socket
|> PhoenixLiveSession.put_session("__idx_token", tokens["__idx_token"])
|> PhoenixLiveSession.put_session("__idx_refresh_token", tokens["__idx_refresh_token"])
|> PhoenixLiveSession.put_session("__idx_id_token", tokens["__idx_id_token"])
|> assign(:tokens, tokens)
end
Am i doing something wrong?
I've been working with this library in development, and now I'm trying to get it running in production (on Gigalixir). I'm not having much luck. Every time I try to update the session, I'm getting key :__live_session_id__ not found in: <socket>
. I'm trying to get better debug/repro information.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.