You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pow注册/登录视图始终显示错误HTML页面问题求助

Phoenix + Pow 授权页面跳转异常

我正在基于Phoenix LiveView开发聊天Web应用,在实现用户授权环节遇到异常:

  • 按配置,“注册”“登录”按钮分别指向/session/new和/registration/new,对应模板文件应位于lib/chat_web/templates/pow/session/new.html.eex与lib/chat_web/templates/pow/registration/new.html.eex
  • 点击按钮后始终跳转到lib/chat_web/templates/layout/app.html.eex页面
  • 删除任意new.html.eex文件,会出现“Could not render "new.html"”错误,说明路由配置正确

使用版本:

  • Elixir 1.14.2
  • Phoenix 1.6.15
  • Pow 1.0.27

相关代码

/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: Routes.pow_registration_path(@conn, :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

  # 会话路由需验证身份
  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>

内容的提问来源于stack exchange,提问作者Artur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 20:47:48