【问题标题】:How do I skip a plug for an specific page如何跳过特定页面的插件
【发布时间】:2015-07-20 21:25:25
【问题描述】:

我正在构建一个带有身份验证的 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

我想跳过 /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

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

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

    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
    

    您可以在幻灯片结尾处阅读更多关于 Phoenix 路由中的宏如何工作的信息,网址为 http://www.chrismccord.com/blog/2014/03/13/write-less-do-more-and-have-fun-with-elixir-macros/

    【讨论】:

      猜你喜欢
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多