Problem to redirect Router path in liveview handle_info

hello, I have a handle_info function on my LiveView which checks user_logout with broadcast or Process.send_after every 3 sec. but redirect doesn’t work and just reload current page , it should be noted I want to load a router of my controller

  def handle_info({:user_logout, user_id}, socket) do
    socket =
    if user_id == socket.assigns.user_id  do
        redirect(socket, to: Routes.auth_path(socket, :login))
    else
      socket
    end
    {:noreply, socket}
  end

it just shows me:

[debug] LiveView session was misconfigured or the user token is outdated.

1) Ensure your session configuration in your endpoint is in a module attribute:

    @session_options [
      ...
    ]

2) Change the `plug Plug.Session` to use said attribute:

    plug Plug.Session, @session_options

3) Also pass the `@session_options` to your LiveView socket:

    socket "/live", Phoenix.LiveView.Socket,
      websocket: [connect_info: [session: @session_options]]

4) Ensure the `protect_from_forgery` plug is in your router pipeline:

    plug :protect_from_forgery

5) Define the CSRF meta tag inside the `<head>` tag in your layout:

    <%= csrf_meta_tag() %>

6) Pass it forward in your app.js:

    let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content");
    let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}});

Hi! This redirect worked for me:

{:error, message} ->
            {:noreply,
             socket
             |> put_flash(:error, message)
             |> redirect(
               to:
                 TestivatorWeb.Router.Helpers.charter_path(TestivatorWeb.Endpoint, :index,
                   shared: true,
                   direction: "asc"
                 )
             )}
1 Like

Is handle_info even receiving the message? My first guess is that the function which broadcasts {:user_logout, user_id} is crashing the LV process, and the message is never getting sent.

1 Like

Yes I had a logical error