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