Find loaded providers for OmniAuth

拈花ヽ惹草 提交于 2019-12-05 06:36:42

OmniAuth::Strategies lists strategies available and registered. Not those that are in 'use'. If you dig through the code of OmniAuth builder, you will see that various strategies are passed onto Rack using use as middleware in the provider block, which makes tracking the strategies harder. Another "pragmatic" approach is to monkey patch OmniAuth Builder and track the providers.

module OmniAuth
  class Builder < ::Rack::Builder
    def provider_patch(klass, *args, &block)
      @@providers ||= []
      @@providers << klass
      old_provider(klass, *args, &block)
    end
    alias old_provider provider
    alias provider provider_patch
    class << self
      def providers
        @@providers
      end
    end
  end
end

Include this patch before configuring your providers. Once all the providers have been loaded, OmniAuth::Builder.providers will give you the array you want.

Even though Developer strategy is available, it is not loaded. It is only loaded if you specify

provider :developer

Adding this here for Devise users, as I needed the same list as iain.

I tried using the accepted answer at the top of my devise initializer in a Rails project, but I got an error (@@providers was not defined).

After looking into the Devise source code, I used the following to get an array of symbols:

Devise.omniauth_configs.keys # => [:facebook, :twitter]
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!