簡體   English   中英

在運行時切換omniauth-shopify-oauth2 gem的提供程序?

[英]Switch provider for the omniauth-shopify-oauth2 gem in runtime?

omn​​iauth-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.combrand2.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM