Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to load current session on redirect #1

Open
bradrf opened this issue Feb 9, 2021 · 9 comments
Open

Fails to load current session on redirect #1

bradrf opened this issue Feb 9, 2021 · 9 comments

Comments

@bradrf
Copy link

bradrf commented Feb 9, 2021

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
  }
@bradrf
Copy link
Author

bradrf commented Feb 9, 2021

Minor update: seems to work fine with a "normal" redirect.

@wmnnd
Copy link
Contributor

wmnnd commented Feb 10, 2021

Could you share the mount callback of your second LiveView?

@bradrf
Copy link
Author

bradrf commented Feb 10, 2021

Definitely!

  def mount(%{"room_id" => room_id} = _params, session, socket) do
    room = Rooms.get_room!(room_id)
    user = Users.get_user!(session["user_id"])

    socket = assign(socket, user: user, room: room)

    if connected?(socket) do
      case RoomMaster.join_room(room.name, user.name, self()) do
        {:ok, ot_room} ->
          {:ok, assign(socket, ot_room: ot_room)}

        {:error, reason} ->
          {:ok, socket |> put_flash(:error, reason) |> redirect(to: "/")}
      end
    else
      {:ok, socket}
    end
  end

@Hanspagh
Copy link

Hanspagh commented May 10, 2021

I am having the same problem. Can I help in any way

@wmnnd
Copy link
Contributor

wmnnd commented Jun 1, 2021

It looks like you’re not calling the maybe_subscribe/2 function in your mount/3 callback.

@eduardozacour
Copy link

I am also having this issue. When I do a live_redirect to another live view, the session map in the mount callback for the view is not updated yet. Only when I do a refresh of the page, the new session is provided to the mount callback in the view. Let me know if there is anything I can do to help, and great job on the library by the way.

@jon-mcclung-fortyau
Copy link

I'm having the same issue as well, even when calling maybe_subscribe/2 the session never persists after a push_redirect. Isn't that the whole point? If I'm not changing up live views I can just use socket.assigns...

@tjhayasaka
Copy link

This is my workaround:

def mount(_params, session, socket) do
  socket = PhoenixLiveSession.maybe_subscribe(socket, session)
  session = get_live_session(socket, session)

  # do something with the session...
end

def get_live_session(socket, session) do
  sid = get_in(socket.private, [:live_session, :id])
  opts = get_in(socket.private, [:live_session, :opts])
  if sid == nil or opts == nil do
    session
  else
    {_, session} = PhoenixLiveSession.get(nil, sid, opts)
    session
  end
end

@tnohtition
Copy link

I've faced the same issue. So the solution is invoking redirect/2 right after the PhoenixLiveSession.put_session/3.
Guessing the session doesn't load on the push_navigate/3. It's required a full page load

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants