[英]Switch provider for the omniauth-shopify-oauth2 gem in runtime?
The initializer for the omniauth-shopify-oauth2 gem is supposed to look like this: omniauth-shopify-oauth2 gem的初始化程序应该看起来像这样:
# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET']
end
However, in our Rails app resides a few different brands who offers the same functionality. 但是,在我们的Rails应用程序中存在着几个提供相同功能的不同品牌。 Throughout the entire app, the
request.domain
of a request determines which brand you are exposed to ( brand1.example.com
, brand2.example.com
, etc.). 在整个应用程序中,请求的
request.domain
决定了您接触的品牌( brand1.example.com
, brand2.example.com
等)。
We can easily store brand specific credentials and redirect the users to the brand specific authorization path: 我们可以轻松存储品牌特定的凭据,并将用户重定向到品牌特定的授权路径:
https://example.myshopify.com/admin/oauth/authorize?client_id=brand1&scope=read_orders,read_products&redirect_uri=https://brand1.example.com/auth/shopify/callback
But I can't figure out how we can have different providers for the middleware, chosen based on the visited request.domain
. 但是我不知道如何为中间件提供不同的提供程序,这些提供程序是基于访问的
request.domain
选择的。 Any idea how to set this up? 知道如何设置吗?
Omniauth provides documentation on Dynamic Providers , which will be helpful here. Omniauth提供了有关Dynamic Providers的文档,这将对您有所帮助。 Something like:
就像是:
# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :shopify, setup: lambda do |env|
# Do logic to get correct credentials for request.
# For example, if you store the credentials on a model called Brand,
# and have it keyed on "subdomain":
request = ActionDispatch::Request.new(env)
brand = Brand.find_by(subdomain: request.subdomain)
env['omniauth.strategy'].options.merge!({
client_id: brand.client_id,
client_secret: brand.client_secret
})
# `site` needs to be set. This is part of the shopify provider setup phase, which we are overriding
env['omniauth.strategy'].options[:client_options][:site] = "https://#{ request.GET['shop'] }"
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.