[英]Switch provider for the omniauth-shopify-oauth2 gem in runtime?
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
選擇的。 知道如何設置嗎?
Omniauth提供了有關Dynamic Providers的文檔,這將對您有所幫助。 就像是:
# 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.