繁体   English   中英

如何跳过特定页面的插件

[英]How do I skip a plug for an specific page

我正在使用身份验证构建Phoenix应用程序。 在我的路由器中我有类似的东西:

pipeline :browser do
    plug :accepts, ["html"]
    plug MyApp.Plugs.Authenticate
end

scope "/", MyApp do
    pipe_through :browser # Use the default browser stack

    get "/", HomeController, :show
    get "/login", SessionsController, :login
    get "/matches", MatchesController, :index
end

我想跳过Authenticate插件/ login,我可以在路由器中执行此操作,还是必须在插件本身中执行此操作?

Plugs.Authenticate看起来像:

def call(conn, _) do
    case Authenticator.find_user(conn) do
        {:ok, user} ->
            assign(conn, :user, user)
        :error ->
            conn
                |> redirect(to: "/login")
                |> halt
    end
end

一种方法是定义一个单独的管道:

pipeline :browser do
    plug :accepts, ["html"]
end

pipeline :auth do
    plug MyApp.Plugs.Authenticate
end

scope "/", MyApp do
    pipe_through [:browser, :auth]

    get "/", HomeController, :show
    get "/matches", MatchesController, :index
end

scope "/", MyApp do
    pipe_through :browser

    get "/login", SessionsController, :login
end

这里有几点需要注意。

1)在需要验证的示例中,管道被链接。

2)只要实际路由不同,您可以多次使用相同的范围,这是因为上面的路由大致编译为:

defmodule MyRouter do
  def match(conn, :get,    ["/"])
  def match(conn, :get,    ["/matches"])
  def match(conn, :get,    ["/login"])
end

您可以在http://www.chrismccord.com/blog/2014/03/13/write-less-do-more-and-have-fun上阅读有关Phoenix路由中的宏如何工作到幻灯片末尾的更多信息。 -with-仙丹的宏/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM