schrockwell / bodyguard

Simple authorization conventions for Phoenix apps

Home Page:https://hexdocs.pm/bodyguard/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Overriding plug

LostKobrakai opened this issue · comments

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.

Great idea – docs updated!