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
但是,在我们的Rails应用程序中,有几个不同的品牌提供相同的功能。在整个应用中,request.domain
请求会确定您所接触的品牌(brand1.example.com
,brand2.example.com
等)。
我们可以轻松存储品牌特定凭据,并将用户重定向到品牌特定授权路径:
https://example.myshopify.com/admin/oauth/authorize?client_id=brand1&scope=read_orders,read_products&redirect_uri=https://brand1.example.com/auth/shopify/callback
但我无法弄清楚如何根据访问过的request.domain
选择不同的中间件提供商。知道如何设置吗?
答案 0 :(得分:0)
# 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