【发布时间】:2020-03-14 05:16:25
【问题描述】:
我之前曾被指向OnmiAuth Dynamic Providers,以便根据访问的域在运行时切换提供程序。我的解决方案是基于omniauth-shopify-oauth2和this great answer:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :shopify,
scope: 'read_orders,read_products',
setup: lambda { |env|
request = ActionDispatch::Request.new(env)
subdomain = "#{request.subdomain}" != "" ? "#{request.subdomain}." : ""
domain = "#{request.domain}"
full_domain = subdomain+domain
shopify_client = Rails.cache.fetch("#{full_domain}_shopify_client")
env['omniauth.strategy'].options.merge!(
{
client_id: shopify_client[:client_id],
client_secret: shopify_client[:client_secret]
}
)
env['omniauth.strategy'].options[:client_options][:site] = "https://#{request.GET['shop']}"
}
end
但现在我还需要能够动态设置范围。所以缓存中的"#{full_domain}_shopify_client" 将包含一个额外的client_permissions 键,其中包含例如'read_orders,read_products' 或 'read_products'。
如何重构我的代码才能做到这一点?
【问题讨论】:
标签: ruby-on-rails ruby omniauth