Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I skip a plug for an specific page

I am building a Phoenix app with authentication. In my router I have something like:

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

I want to skip the Authenticate plug for /login, can I do this in the router or do I have to do this in the Plug itself?

Plugs.Authenticate looks like:

def call(conn, _) do
    case Authenticator.find_user(conn) do
        {:ok, user} ->
            assign(conn, :user, user)
        :error ->
            conn
                |> redirect(to: "/login")
                |> halt
    end
end
like image 677
Sebastian Avatar asked Jul 20 '15 12:07

Sebastian


People also ask

How do I disable a plugin on a specific page?

To selectively disable plugins on specific pages in WordPress, install the Asset CleanUp or Perfmatters plugin and use the script manager to disable plugins from specific content. RegEx can be used to disable plugins using URL patterns. You can also do this with CSS, JS, and fonts.

How do I bypass plugins in Logic?

You can bypass plug-ins several different ways: Place the pointer over the plug-in slot in a channel strip, then click the Bypass button so the slot dims. Option-click the plug-in slot so the slot dims. In the plug-in window, click the Bypass button so the slot dims.

How do I remove unwanted plugins?

Start by navigating to the Plugins area of your dashboard and finding the plugin you want to remove within the list. If you look under the plugin's name, you'll find the Deactivate option, which just takes a click to use. You'll notice there's no uninstall option listed beneath the plugin's name.


1 Answers

One way to do this is to define a separate pipeline:

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

There are a couple things to note here.

1) The pipelines are being chained in the example where authentication is required.

2) You can use the same scope multiple times as long as the actual route is distinct this is because the routes above compile roughly to:

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

You can read more about how the macros in Phoenix routing work towards the end of the slides at http://www.chrismccord.com/blog/2014/03/13/write-less-do-more-and-have-fun-with-elixir-macros/

like image 131
Gazler Avatar answered Jan 02 '23 20:01

Gazler