Skip to content

Commit

Permalink
Log when navigate fails
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Nov 22, 2024
1 parent 09f7a84 commit edf8a80
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/phoenix_live_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,8 @@ defmodule Phoenix.LiveView do
immediately invoked to handle the change of params and URL state.
Then the new state is pushed to the client, without reloading the
whole page while also maintaining the current scroll position.
For live navigation to another LiveView, use `push_navigate/2`.
For live navigation to another LiveView in the same `live_session`,
use `push_navigate/2`. Otherwise, use `redirect/2`.
## Options
Expand All @@ -1051,7 +1052,7 @@ defmodule Phoenix.LiveView do
end

@doc """
Annotates the socket for navigation to another LiveView.
Annotates the socket for navigation to another LiveView in the same `live_session`.
The current LiveView will be shutdown and a new one will be mounted
in its place, without reloading the whole page. This can
Expand Down
17 changes: 15 additions & 2 deletions lib/phoenix_live_view/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1532,10 +1532,23 @@ defmodule Phoenix.LiveView.Channel do
defp authorize_session(%Session{} = session, endpoint, %{"redirect" => url}) do
if redir_route = session_route(session, endpoint, url) do
case Session.authorize_root_redirect(session, redir_route) do
{:ok, %Session{} = new_session} -> {:ok, new_session, redir_route, url}
{:error, :unauthorized} = err -> err
{:ok, %Session{} = new_session} ->
{:ok, new_session, redir_route, url}

:error ->
Logger.warning(
"navigate event to #{inspect(url)} failed because you are redirecting across live_sessions. " <>
"A full page reload will be performed instead"
)

{:error, :unauthorized}
end
else
Logger.warning(
"navigate event to #{inspect(url)} failed because the URL does not point to a LiveView. " <>
"A full page reload will be performed instead"
)

{:error, :unauthorized}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/phoenix_live_view/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Phoenix.LiveView.Session do
{:ok, replace_root(session, route.view, self())}

true ->
{:error, :unauthorized}
:error
end
end

Expand Down

0 comments on commit edf8a80

Please sign in to comment.