config = Rails.application.config_for(:configuration)

OmniAuth.config.add_camelization 'qq', 'QQ' if config.dig("oauth", "qq")
# OmniAuth.config.add_camelization 'github', 'GitHub' if config.dig("oauth", "github")
# OmniAuth.config.add_camelization 'gitee', 'Gitee' if config.dig("oauth", "gitee")
# OmniAuth.config.add_camelization 'wechat', 'Wechat' if config.dig("oauth", "wechat")
OmniAuth.config.logger = Rails.logger
OmniAuth.config.allowed_request_methods = %i[get post]
OmniAuth.config.before_request_phase = nil
OmniAuth.config.before_callback_phase = nil
OmniAuth.config.on_failure = Proc.new { |env|
  OmniAuth::FailureEndpoint.new(env).redirect_to_failure
}

Rails.application.config.middleware.use OmniAuth::Builder do
  if config.dig("oauth", "qq")
    provider :qq, config.dig("oauth", "qq", "appid"), config.dig("oauth", "qq", "secret"), { provider_ignores_state: true }
  end
  if config.dig("oauth", "github").present?
    provider :github, config.dig("oauth", "github", "appid"), config.dig("oauth", "github", "secret"), { provider_ignores_state: true, scope: "user:email" }
  end
  if config.dig("oauth", "gitee").present?
    provider :gitee, config.dig("oauth", "gitee", "appid"), config.dig("oauth", "gitee", "secret"), { provider_ignores_state: true, scope: "user_info emails" }
  end
  if config.dig("oauth", "wechat").present?
    provider :wechat, config.dig("oauth", "wechat", "appid"), config.dig("oauth", "wechat", "secret"), { provider_ignores_state: true, scope: "snsapi_login" }
  end
end
