Skip to content

Commit 6803694

Browse files
committed
Add a default value for the setup option.
1 parent 35eecaa commit 6803694

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

README.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,11 @@ You can configure the scope, which you pass in to the `provider` method via a `H
3232

3333
* `scope`: A comma-separated list of permissions you want to request from the user. See [the Shopify API docs](http://docs.shopify.com/api/tutorials/oauth) for a full list of available permissions.
3434

35-
* `setup`: A lambda which dynamically sets the `site`. You must initiate the OmniAuth process by passing in a `shop` query parameter of the shop you're requesting permissions for. Ex. http://myapp.com/auth/shopify?shop=example.myshopify.com
36-
3735
For example, to request `read_products`, `read_orders` and `write_content` permissions and display the authentication page:
3836

3937
```ruby
4038
Rails.application.config.middleware.use OmniAuth::Builder do
41-
provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET'],
42-
:scope => 'read_products,read_orders,write_content',
43-
:setup => lambda { |env| params = Rack::Utils.parse_query(env['QUERY_STRING'])
44-
env['omniauth.strategy'].options[:client_options][:site] = "https://#{params['shop']}" }
39+
provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET'], :scope => 'read_products,read_orders,write_content'
4540
end
4641
```
4742

example/config.ru

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ end
3838
use Rack::Session::Cookie, secret: SecureRandom.hex(64)
3939

4040
use OmniAuth::Builder do
41-
provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET'],
42-
:scope => SCOPE,
43-
:setup => lambda { |env| params = Rack::Utils.parse_query(env['QUERY_STRING'])
44-
env['omniauth.strategy'].options[:client_options][:site] = "https://#{params['shop']}" }
41+
provider :shopify, ENV['SHOPIFY_API_KEY'], ENV['SHOPIFY_SHARED_SECRET'], :scope => SCOPE
4542
end
4643

4744
run App.new

lib/omniauth/strategies/shopify.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ class Shopify < OmniAuth::Strategies::OAuth2
1313
}
1414

1515
option :callback_url
16-
16+
1717
option :provider_ignores_state, true
1818
option :myshopify_domain, 'myshopify.com'
1919

20+
option :setup, proc { |env|
21+
request = Rack::Request.new(env)
22+
env['omniauth.strategy'].options[:client_options][:site] = "https://#{request.GET['shop']}"
23+
}
24+
2025
uid { URI.parse(options[:client_options][:site]).host }
2126

2227
def valid_site?

test/integration_test.rb

+10-7
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,16 @@ def test_callback
7474
end
7575

7676
def test_provider_options
77-
build_app scope: 'read_products,read_orders,write_content', callback_path: '/admin/auth/legacy/callback', myshopify_domain: 'myshopify.dev:3000'
78-
response = authorize('snowdevil.myshopify.dev:3000')
77+
build_app scope: 'read_products,read_orders,write_content',
78+
callback_path: '/admin/auth/legacy/callback',
79+
myshopify_domain: 'myshopify.dev:3000',
80+
setup: lambda { |env|
81+
shop = Rack::Request.new(env).GET['shop']
82+
shop += ".myshopify.dev:3000" unless shop.include?(".")
83+
env['omniauth.strategy'].options[:client_options][:site] = "https://#{shop}"
84+
}
85+
86+
response = authorize('snowdevil')
7987
assert_equal 302, response.status
8088
assert_match /\A#{Regexp.quote("https://snowdevil.myshopify.dev:3000/admin/oauth/authorize?")}/, response.location
8189
redirect_params = Rack::Utils.parse_query(URI(response.location).query)
@@ -92,11 +100,6 @@ def build_app(options={})
92100
}
93101

94102
app = OmniAuth::Builder.new(app) do
95-
options[:setup] ||= lambda { |env|
96-
params = Rack::Utils.parse_query(env['QUERY_STRING'])
97-
env['omniauth.strategy'].options[:client_options][:site] = "https://#{params['shop']}"
98-
}
99-
100103
provider :shopify, '123', '53cr3tz', options
101104
end
102105
@app = Rack::Session::Cookie.new(app, secret: SecureRandom.hex(64))

0 commit comments

Comments
 (0)