我想知道如何使用 Phoenix 在同一个根“/”中实现两个页面?一种用于未经身份验证的用户,另一种用于经过身份验证的用户。发生这种情况的用例示例是 LinkedIn 和 Facebook,其中登录页面在“/”上提供,应用程序在登录后在“/”上提供。
我使用 Guardian 进行身份验证,并将我的路由器设置为:
pipeline :auth do
plug Guardian.Plug.EnsureAuthenticated, handler: App.AuthHandler
end
pipeline :unauth do
plug Guardian.Plug.EnsureNotAuthenticated, handler: App.AuthHandler
end
scope "/", App.Web do
pipe_through [:browser, :unauth]
get "/", FrontController, :index
end
scope "/", App.Web do
pipe_through [:browser, :auth]
get "/page", ApplicationController, :index
end
FrontController 服务于未经身份验证的用户可访问的页面(例如登录页面),而 ApplicationController 服务于实际应用程序。
当 ApplicationController 提供“/”而不是“/page”时,会抛出“此子句无法匹配,因为第 1 行的前一个子句始终匹配”错误。
我想象使用 if 语句和一个控制器来为两个页面提供服务,不幸的是我找不到有关如何实施这种策略的文档。