I am working on a project which is a chat web application based on this article: Phoenix LiveView Tutorial: Adding Phoenix PubSub and Pow Authentication to Messenger | Curiosum. I have reached the part where user authorization is implemented and I have encourted a weird problem. According to the article and it’s github representation everything is set up properly. There are two buttons - “sign up” and “register” leading to /session/new and /registration/new respectively. Their html representations are in paths lib/chat_web/templates/pow/session/new.html.eex and lib/chat_web/templates/pow/registration/new.html.eex as they should be. But when I press those buttons they keep redirecting me to the same html page which is lib/chat_web/templates/layout/app.html.eex. If I delete and of the new.html.eex files redirection ends up in Could not render “new.html” error which suggests to me that routing is correct. I really am out of ideas what my be the cause of this problem. Link to branch in my repository: GitHub - ArturMarekNowak/Chat at wip/authorization
/lib/chat_web/templates/layout/app.html.eex
<%= if Pow.Plug.current_user(@conn) do %>
<li><%= link "Profile", to: Routes.pow_registration_path(@conn, :edit) %></li>
<li><%= link "Sign out", to: Routes.pow_session_path(@conn, :delete), method: :delete %></li>
<% else %>
<li><%= link "Register", to: "/registration/new" %></li>
<li><%= link "Sign in", to: Routes.pow_session_path(@conn, :new) %></li>
<% end %>
/lib/chat_web/router.ex
defmodule ChatWeb.Router do
use ChatWeb, :router
use Pow.Phoenix.Router
import Phoenix.LiveView.Router
pipeline :browser do
plug(:accepts, ["html"])
plug(:fetch_session)
plug(:fetch_live_flash)
plug(:protect_from_forgery)
plug(:put_secure_browser_headers)
plug(:put_root_layout, {ChatWeb.LayoutView, :root})
end
pipeline :api do
plug(:accepts, ["json"])
end
pipeline :protected do
plug(Pow.Plug.RequireAuthenticated,
error_handler: Pow.Phoenix.PlugErrorHandler
)
end
scope "/" do
pipe_through(:browser)
pow_routes()
end
scope "/", ChatWeb do
pipe_through(:browser)
get("/", PageController, :index)
end
# Make conversation routes protected by requiring authentication
scope "/", ChatWeb do
pipe_through([:browser, :protected])
resources("/conversations", ConversationController)
live("/conversations/:conversation_id/users/:user_id", ConversationLive, as: :conversation)
end
end
/lib/chat_web/views/pow/session_view.ex
defmodule ChatWeb.Pow.SessionView do
use ChatWeb, :view
end
/lib/chat_web/templates/pow/session/new.html.eex
<h1>Register</h1>
<%= form_for @changeset, @action, [as: :user], fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %>
<%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %>
<%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %>
<%= label f, :nickname %>
<%= text_input f, :nickname %>
<%= error_tag f, :nickname %>
<%= label f, :password %>
<%= password_input f, :password %>
<%= error_tag f, :password %>
<%= label f, :confirm_password %>
<%= password_input f, :confirm_password %>
<%= error_tag f, :confirm_password %>
<div>
<%= submit "Register" %>
</div>
<% end %>
<span><%= link "Sign in", to: Routes.pow_session_path(@conn, :new) %></span>