paulanthonywilson/basic_auth

Anyway I can use this in Phoenix router definition?

Closed this issue · 3 comments

Hi @paulanthonywilson ,
First of all, thank you so much for making this library. It has been great help for me.
I wonder if could use this in my Phoenix router definition? I would like to apply basic auth for a certain router scope, so that I don't have to write the plug code in every controller under that scope.
Sort of like this.

defmodule MyApp.Router do
  use MyApp, :router

  pipeline :admin_auth do
    plug(:basic_auth, ...)
  end

  scope "/admin", as: :admin do
    pipe_through([:browser, :admin_auth])
    # My router definition
  end

Thank you!

Hi @ChrisZou,
You can use it in a Phoenix router definition. I've used it like this in a Phoenix app's router:

pipeline :authentication do
  plug BasicAuth, use_config: {:web, :authentication}
end

scope "/", Web do
  pipe_through [:browser, :authentication]

  get "/", PageController, :index
end

And I used it in a learning project with a similar example here.

Seems to be covered.

@picandocodigo Thank you! It works. I guess I don't understand plug enough and didn't read the doc carefully enough. 😅