Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have a Phoenix Plug just for one route?

In Phoenix I have my routes as follow :

  scope "/", ManaWeb do
    pipe_through [:browser, :auth]
    get "/register",  RegistrationController, :new
    post "/register", RegistrationController, :register
  end

However I would like to set a Plug for the last route (POST).

How would I go about that with current tools ?

like image 320
thodg Avatar asked Oct 17 '25 13:10

thodg


2 Answers

Another solution would be using the plug directly in the controller

defmodule ManaWeb.RegistrationController do
  # import the post_plug...
  plug :post_plug when action in [:register]

  def register(conn, params) do
    # ...
  end
end
like image 96
fhdhsni Avatar answered Oct 19 '25 11:10

fhdhsni


As it is stated in the documentation for Phoenix.Router.pipeline/2

Every time pipe_through/1 is called, the new pipelines are appended to the ones previously given.

That said, this would work:

scope "/", ManaWeb do
  pipe_through [:browser, :auth]
  get "/register",  RegistrationController, :new

  pipe_through :post_plug
  post "/register", RegistrationController, :register
end
like image 28
Aleksei Matiushkin Avatar answered Oct 19 '25 13:10

Aleksei Matiushkin