Giter VIP home page Giter VIP logo

phoenix_live_session's People

Contributors

jvantuyl avatar wmnnd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phoenix_live_session's Issues

Fails to load current session on redirect

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
  }

put_session should check socket.connected?

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.

put_session is modifying session, but somehow not notifying listeners *only in beginning*

Okay so I have a very concrete and repeatable scenario where seemingly listeners aren't getting notified, where normally they do:

  1. Login
  2. Fire a specific session changing click event
  3. See no calls to 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.

Useage in components

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.

Pattern Match Failure when calling maybe_subscribe/2

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..

PhoenixLiveSession.put_session not working as expected

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?

Deploy to Gigalixir

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.