Overriding plug
LostKobrakai opened this issue · 1 comments
LostKobrakai commented
Currently it's suggested to use the app env for providing default values for the Authorize
plug:
### Default Plug Options
Application-wide defaults for the above options can be specified in the application config. For
example, if you're using Phoenix with Pow for authentication, you might want to specify:
config :bodyguard, Bodyguard.Plug.Authorize,
action: {Phoenix.Controller, :action_name},
user: {Pow.Plug, :current_user}
https://github.com/schrockwell/bodyguard/blob/develop/lib/bodyguard/plug/authorize.ex#L26-L33
I'm wondering if it would be better to suggest just wrapping it in another plug like so:
defmodule MyAppWeb.Authorize do
def init(opts) do
opts
|> Keyword.put_new(:action, {Phoenix.Controller, :action_name})
|> Keyword.put_new(:user, {Pow.Plug, :current_user})
|> Bodyguard.Plug.Authorize.init()
end
def call(conn, opts) do
Bodyguard.Plug.Authorize.call(conn, opts)
end
end
It's more flexible and also isn't limited to a single set of defaults.
schrockwell commented
Great idea – docs updated!