How to convert socket to conn?

Hello! I would like to redirect from live page to usual phoenix (non-live) page, so i want to convert live Socket to Plug.Conn. How can I do this?
For instance, I have user registration handle event (live) and log_in_user function (generated by phx.gen.auth, non live, accept conn, but I have only socket in handle event).

def handle_event("register", %{"user" => user_params}, socket) do
  case Accounts.register_user(user_params) do
    {:ok, user} ->
      {:noreply,
       socket
       |> put_flash(:info, "user created")
       |> UserAuth.log_in_user(user)
       |> push_redirect(to: Routes.live_path(socket, Project.PageLive))
       }
    {:error, %Ecto.Changeset{} = changeset} ->
      {:noreply, assign(socket, changeset: changeset)}
    end
  end
  def log_in_user(conn, user, params \\ %{}) do
    token = Accounts.generate_user_session_token(user)
    user_return_to = get_session(conn, :user_return_to)

    conn
    |> renew_session()
    |> put_session(:user_token, token)
    |> put_session(:live_socket_id, "users_sessions:#{Base.url_encode64(token)}")
    |> maybe_write_remember_me_cookie(token, params)
    |> redirect(to: user_return_to || signed_in_path(conn))
  end

So how can I adapt UserAuth.log_in_user to work with Socket (LiveView)?

1 Like

Short answer: You can’t. Session handling is not available through the websocket connection of liveview. For logging in a user you’re best of going through a controller doing the necessary session setup work.

2 Likes