Good night
I am currently trying to display the username that is attached to my template.
Ex:
User will log in to my page, after logging in he will be able to see the exit button and next to his username.
I have made the following changes:
Could help me identify where the error is?
Helpers
defmodule MyprojectWeb.Helpers.Auth do
def signed_in?(conn) do
user_id = Plug.Conn.get_session(conn, :current_user_id)
if user_id, do: !!Myproject.Repo.get(Myproject.Accounts.User, user_id)
end
end
_navbar.html.eex
%= if signed_in?(@conn) do %>
**<%= if signed_in?(@conn) == @conn.assigns[:users] do %>**
** <h1> <%= link signed_in?(@conn).nome %></h1>**
** <% end %></span> **
<%= link "Sair", to: session_path(@conn, :delete), method: :delete, class: "btn btn-outline-success my-2 my-sm-0" %>
<% else %>
<%= link "Login", to: session_path(@conn, :new), class: "btn btn-outline-success my-2 my-sm-0" %>
<% end %>
At the moment the only thing that is not working and get the name of the user.
Instead of fetching user and throwing it away, fetch user record from user id and assign it to something, you’ll probably want to do it in a plug. Then just use:
user = Myproject.Repo.get(Myproject.Accounts.User, user_id)
Plug.Conn.assign(:current_user, user)
Myproject.Repo.get might return nil, and which would make conn.assigns[:current_user].name evaluate to nil.name which would fail with a not-quite-obvious error message. Or if :current_user assign is not set, conn.assigns[:current_user].name would again evaluate to nil.name.
So I’d suggest to add a helper function like into the view module that renders the navbar
@spec current_user_name(Plug.Conn.t()) :: String.t() | nil
def current_user_name(conn) do
if current_user = conn.assigns[:current_user] do
current_user.name
end
end
defmodule MyprojectWeb.IndicacaoView do
# ...
@spec current_user_name(Plug.Conn.t()) :: String.t() | nil
def current_user_name(conn) do
if current_user = conn.assigns[:current_user] do
current_user.name
end
end
end
# in the template
Name: <%= current_user_name(@conn) %>
plug :check_auth when action in [:index, :new, :create, :edit, :update, :delete]
defp check_auth(conn, _args) do
if user_id = get_session(conn, :current_user_id) do
current_user = Myproject.Accounts.get_user!(user_id)
conn
|> assign(:current_user, current_user)
else
conn
|> put_flash(:error, "You need to be signed in to access that page.")
|> redirect(to: Routes.page_path(conn, :index))
|> halt()
end
end