LiveView, phx.gen.auth: how to set user_auth :user_return_to in LiveView

Phoenix: 1.6.6
LiveView:0.17.6

PROBLEM: After login, the user is always taken back to the homepage (localhost4000).

The user is initially in a LiveView page that does not require a login (LV1) and is not the homepage. The user clicks on a link to redirect them to a LiveView that requires authentication (LV2-A). Phx.gen.auth processes the login correctly, but always redirects the user back to the homepage. So the user is not redirected to LV2-A or even back to LV1 where they started.

I can see that user_auth accesses :user_return_to which is set in the conn. That must be set to the home page. I need to change it to the destination LV, but I don’t have access to conn in a LiveView.

How can I set the :user_return_to within a LiveView?

NOTE: I am on Phoenix 1.6.6 where phx.gen.auth is built in. I followed the instructions in hexdocs and added an “on_mount” in all of my LVs that require authentication. I define the “on_mount” in my live_helpers.ex and it looks like this:

def on_mount(:default, _params, session, socket) do

    socket = assign_current_user(socket, session)

    if socket.assigns.current_user == nil do
      {:halt, redirect(socket, to: "/users/log_in")}
    else
      {:cont, socket}
    end
  end

You could add a ?continue= url param to your login links holding the current request path and handle that in UserAuth.log_in_user/3

user_return_to = params["continue"] || get_session(conn, :user_return_to)