Can`t see logged in user in different templates

Hey there!

I have a real headache problem. I do have an authentication page, I can log in user/ create user/ view users. and it all works as intended. But on my main page, I cant see my user (I’m not in it). But when I back to live pages for viewing users I’m still logged in.
Here are my routes:

defmodule ContinentalDevLiveWeb.Router do
  use ContinentalDevLiveWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, {ContinentalDevLiveWeb.LayoutView, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", ContinentalDevLiveWeb do
    pipe_through :browser

    live "/", PageLive, :index
    live "/foo", FooLive
    live "/users", UserLive.Index
    live "/users/new", UserLive.New
    live "/users/:id/edit", UserLive.Edit
    live "/users/:id", UserLive.Show

    get("/forgot", SessionController, :forgot)
    get("/login", SessionController, :new)
    get("/login/:token/email/:email", SessionController, :create_from_token)
    get("/logout", SessionController, :delete)
    post("/forgot", SessionController, :reset_pass)

    resources(
      "/sessions",
      SessionController,
      only: [:new, :create, :delete],
      singleton: true
    )

  end

  # Other scopes may use custom stacks.
  # scope "/api", ContinentalDevLiveWeb do
  #   pipe_through :api
  # end

  # Enables LiveDashboard only for development
  #
  # If you want to use the LiveDashboard in production, you should put
  # it behind authentication and allow only admins to access it.
  # If your application does not have an admins-only section yet,
  # you can use Plug.BasicAuth to set up some basic authentication
  # as long as you are also using SSL (which you should anyway).
  if Mix.env() in [:dev, :test] do
    import Phoenix.LiveDashboard.Router

    scope "/" do
      pipe_through :browser
      live_dashboard "/dashboard", metrics: ContinentalDevLiveWeb.Telemetry
    end
  end
end

As you can see I have live routes and my problem is in

  live "/", PageLive, :index

here’s what i have in root.html.leex:

<body>
    <header>
      <section class="container">
<nav role="navigation">
          <ul>
          <%= require Logger
          Logger.info(@conn.assigns[:current_user]) %>
            <%= if @conn.assigns[:current_user] do %>
              <li>
              <%=  link(@current_user.username, to: "/users/#{@current_user.id}") %>
                <%= link "Log out", to: "/logout" %>
              </li>
            <% else %>
              <li><%= link "Sign in", to: "/login" %></li>
              <li><%= link "Sign up", to: "/users/new" %></li>

            <% end %>
          </ul>
        </nav>
        <a href="https://phoenixframework.org/" class="phx-logo">
          <img src="<%= Routes.static_path(@conn, "/images/phoenix.png") %>" alt="Phoenix Framework Logo"/>
        </a>
      </section>
    </header>
    <%= @inner_content %>
  </body>

As you can see I’ve added

 <%= require Logger
          Logger.info(@conn.assigns[:current_user]) %>

to see it sees assigns. It still puts ok, but I`m not logged in.

I think I don’t get the general idea of live view so I would be glad if you correct me :nerd_face:

Do you see anything if you use @socket instead of @conn in your templates?

i got this when i change conn to socket

assign @socket not available in eex template

but i have root.html.leex

Ah, I see. Try checking for user in live.html.leex using @socket. Would it show anything different? Also, how do you populate conn/socket assigns with current user?

Live layouts — Phoenix LiveView v0.16.3 might be worth checking out.

Solved. The problem was in my PageLive.mount. Now my mount looks like this:

  def mount(_params, %{"user_id" => user_id}, socket) do
    if connected?(socket), do: ContinentalDevLive.Accounts.subscribe()
    current_user = Accounts.get_user!(user_id)
    admin_user = ContinentalDevLiveWeb.Auth.is_admin?(current_user)

    {:ok,
    socket
     |> assign(current_user: current_user, admin_user: admin_user)}
  end

  def mount(_params, _session, socket) do
    if connected?(socket), do: ContinentalDevLive.Accounts.subscribe()

    {:ok, socket |> assign(current_user: nil, admin_user: nil)}
  end

thank you for helping me out!