shopify_app 7.2.0 → 17.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +5 -5
  2. data/.babelrc +5 -0
  3. data/.github/CODEOWNERS +1 -0
  4. data/.github/ISSUE_TEMPLATE/bug-report.md +63 -0
  5. data/.github/ISSUE_TEMPLATE/config.yml +1 -0
  6. data/.github/ISSUE_TEMPLATE/feature-request.md +33 -0
  7. data/.github/PULL_REQUEST_TEMPLATE.md +22 -0
  8. data/.github/probots.yml +2 -0
  9. data/.github/workflows/build.yml +38 -0
  10. data/.github/workflows/release.yml +24 -0
  11. data/.github/workflows/rubocop.yml +22 -0
  12. data/.gitignore +4 -1
  13. data/.nvmrc +1 -0
  14. data/.rubocop.yml +18 -0
  15. data/.ruby-version +1 -0
  16. data/CHANGELOG.md +465 -0
  17. data/CONTRIBUTING.md +76 -0
  18. data/Gemfile +7 -0
  19. data/Gemfile.lock +256 -0
  20. data/README.md +73 -288
  21. data/Rakefile +1 -0
  22. data/SECURITY.md +59 -0
  23. data/app/assets/images/storage_access.svg +1 -0
  24. data/app/assets/javascripts/shopify_app/enable_cookies.js +3 -0
  25. data/app/assets/javascripts/shopify_app/itp_helper.js +40 -0
  26. data/app/assets/javascripts/shopify_app/partition_cookies.js +8 -0
  27. data/app/assets/javascripts/shopify_app/redirect.js +33 -0
  28. data/app/assets/javascripts/shopify_app/request_storage_access.js +3 -0
  29. data/app/assets/javascripts/shopify_app/storage_access.js +154 -0
  30. data/app/assets/javascripts/shopify_app/storage_access_redirect.js +17 -0
  31. data/app/assets/javascripts/shopify_app/top_level.js +2 -0
  32. data/app/assets/javascripts/shopify_app/top_level_interaction.js +11 -0
  33. data/app/controllers/concerns/shopify_app/authenticated.rb +16 -0
  34. data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +26 -0
  35. data/app/controllers/concerns/shopify_app/require_known_shop.rb +39 -0
  36. data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +32 -0
  37. data/app/controllers/shopify_app/authenticated_controller.rb +5 -5
  38. data/app/controllers/shopify_app/callback_controller.rb +196 -0
  39. data/app/controllers/shopify_app/extension_verification_controller.rb +15 -0
  40. data/app/controllers/shopify_app/sessions_controller.rb +190 -2
  41. data/app/controllers/shopify_app/webhooks_controller.rb +16 -7
  42. data/app/views/shopify_app/partials/_button_styles.html.erb +109 -0
  43. data/app/views/shopify_app/partials/_card_styles.html.erb +33 -0
  44. data/app/views/shopify_app/partials/_empty_state_styles.html.erb +98 -0
  45. data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
  46. data/app/views/shopify_app/partials/_layout_styles.html.erb +182 -0
  47. data/app/views/shopify_app/partials/_typography_styles.html.erb +35 -0
  48. data/app/views/shopify_app/sessions/enable_cookies.html.erb +70 -0
  49. data/app/views/shopify_app/sessions/new.html.erb +39 -83
  50. data/app/views/shopify_app/sessions/request_storage_access.html.erb +68 -0
  51. data/app/views/shopify_app/sessions/top_level_interaction.html.erb +63 -0
  52. data/app/views/shopify_app/shared/redirect.html.erb +23 -0
  53. data/config/locales/cs.yml +23 -0
  54. data/config/locales/da.yml +20 -0
  55. data/config/locales/de.yml +22 -0
  56. data/config/locales/en.yml +12 -1
  57. data/config/locales/es.yml +21 -3
  58. data/config/locales/fi.yml +20 -0
  59. data/config/locales/fr.yml +23 -0
  60. data/config/locales/hi.yml +23 -0
  61. data/config/locales/it.yml +21 -0
  62. data/config/locales/ja.yml +17 -0
  63. data/config/locales/ko.yml +19 -0
  64. data/config/locales/ms.yml +22 -0
  65. data/config/locales/nb.yml +21 -0
  66. data/config/locales/nl.yml +21 -0
  67. data/config/locales/pl.yml +21 -0
  68. data/config/locales/pt-BR.yml +21 -0
  69. data/config/locales/pt-PT.yml +22 -0
  70. data/config/locales/sv.yml +21 -0
  71. data/config/locales/th.yml +20 -0
  72. data/config/locales/tr.yml +22 -0
  73. data/config/locales/vi.yml +22 -0
  74. data/config/locales/zh-CN.yml +16 -0
  75. data/config/locales/zh-TW.yml +16 -0
  76. data/config/routes.rb +12 -1
  77. data/docs/Quickstart.md +31 -0
  78. data/docs/Releasing.md +21 -0
  79. data/docs/Troubleshooting.md +16 -0
  80. data/docs/Upgrading.md +110 -0
  81. data/docs/shopify_app/authentication.md +124 -0
  82. data/docs/shopify_app/engine.md +82 -0
  83. data/docs/shopify_app/generators.md +127 -0
  84. data/docs/shopify_app/handling-access-scopes-changes.md +8 -0
  85. data/docs/shopify_app/script-tags.md +28 -0
  86. data/docs/shopify_app/session-repository.md +88 -0
  87. data/docs/shopify_app/testing.md +38 -0
  88. data/docs/shopify_app/webhooks.md +72 -0
  89. data/karma.conf.js +44 -0
  90. data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +47 -0
  91. data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +11 -0
  92. data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +40 -0
  93. data/lib/generators/shopify_app/add_marketing_activity_extension/templates/marketing_activities_controller.rb +62 -0
  94. data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +5 -4
  95. data/lib/generators/shopify_app/add_webhook/templates/{webhook_job.rb → webhook_job.rb.tt} +5 -0
  96. data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +4 -3
  97. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_controller.rb +3 -3
  98. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_route.rb +10 -9
  99. data/lib/generators/shopify_app/app_proxy_controller/templates/index.html.erb +2 -2
  100. data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +15 -0
  101. data/lib/generators/shopify_app/authenticated_controller/templates/authenticated_controller.rb +5 -0
  102. data/lib/generators/shopify_app/controllers/controllers_generator.rb +2 -1
  103. data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +31 -9
  104. data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +6 -1
  105. data/lib/generators/shopify_app/home_controller/templates/index.html.erb +70 -6
  106. data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +11 -0
  107. data/lib/generators/shopify_app/install/install_generator.rb +78 -27
  108. data/lib/generators/shopify_app/install/templates/_flash_messages.html.erb +1 -13
  109. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +12 -11
  110. data/lib/generators/shopify_app/install/templates/flash_messages.js +24 -0
  111. data/lib/generators/shopify_app/install/templates/omniauth.rb +3 -1
  112. data/lib/generators/shopify_app/install/templates/session_store.rb +4 -0
  113. data/lib/generators/shopify_app/install/templates/shopify_app.js +15 -0
  114. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +25 -0
  115. data/lib/generators/shopify_app/install/templates/shopify_app_index.js +2 -0
  116. data/lib/generators/shopify_app/install/templates/shopify_provider.rb.tt +8 -0
  117. data/lib/generators/shopify_app/install/templates/user_agent.rb +6 -0
  118. data/lib/generators/shopify_app/products_controller/products_controller_generator.rb +19 -0
  119. data/lib/generators/shopify_app/products_controller/templates/products_controller.rb +8 -0
  120. data/lib/generators/shopify_app/rotate_shopify_token_job/rotate_shopify_token_job_generator.rb +16 -0
  121. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token.rake +17 -0
  122. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token_job.rb +42 -0
  123. data/lib/generators/shopify_app/routes/routes_generator.rb +1 -0
  124. data/lib/generators/shopify_app/routes/templates/routes.rb +10 -9
  125. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +42 -14
  126. data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
  127. data/lib/generators/shopify_app/shop_model/templates/db/migrate/{create_shops.rb → create_shops.erb} +1 -1
  128. data/lib/generators/shopify_app/shop_model/templates/shop.rb +6 -2
  129. data/lib/generators/shopify_app/shopify_app_generator.rb +5 -3
  130. data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
  131. data/lib/generators/shopify_app/user_model/templates/db/migrate/create_users.erb +16 -0
  132. data/lib/generators/shopify_app/user_model/templates/user.rb +8 -0
  133. data/lib/generators/shopify_app/user_model/templates/users.yml +4 -0
  134. data/lib/generators/shopify_app/user_model/user_model_generator.rb +70 -0
  135. data/lib/generators/shopify_app/views/views_generator.rb +2 -1
  136. data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
  137. data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
  138. data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
  139. data/lib/shopify_app/configuration.rb +69 -5
  140. data/lib/shopify_app/{app_proxy_verification.rb → controller_concerns/app_proxy_verification.rb} +4 -9
  141. data/lib/shopify_app/controller_concerns/csrf_protection.rb +15 -0
  142. data/lib/shopify_app/controller_concerns/embedded_app.rb +20 -0
  143. data/lib/shopify_app/controller_concerns/itp.rb +45 -0
  144. data/lib/shopify_app/controller_concerns/localization.rb +23 -0
  145. data/lib/shopify_app/controller_concerns/login_protection.rb +244 -0
  146. data/lib/shopify_app/controller_concerns/payload_verification.rb +24 -0
  147. data/lib/shopify_app/controller_concerns/webhook_verification.rb +23 -0
  148. data/lib/shopify_app/engine.rb +40 -0
  149. data/lib/shopify_app/jobs/scripttags_manager_job.rb +16 -0
  150. data/lib/shopify_app/{webhooks_manager_job.rb → jobs/webhooks_manager_job.rb} +3 -2
  151. data/lib/shopify_app/{scripttags_manager.rb → managers/scripttags_manager.rb} +25 -8
  152. data/lib/shopify_app/{webhooks_manager.rb → managers/webhooks_manager.rb} +6 -5
  153. data/lib/shopify_app/middleware/jwt_middleware.rb +42 -0
  154. data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +34 -0
  155. data/lib/shopify_app/omniauth/omniauth_configuration.rb +64 -0
  156. data/lib/shopify_app/session/in_memory_session_store.rb +31 -0
  157. data/lib/shopify_app/session/in_memory_shop_session_store.rb +16 -0
  158. data/lib/shopify_app/session/in_memory_user_session_store.rb +16 -0
  159. data/lib/shopify_app/session/jwt.rb +63 -0
  160. data/lib/shopify_app/session/null_user_session_store.rb +22 -0
  161. data/lib/shopify_app/session/session_repository.rb +56 -0
  162. data/lib/shopify_app/session/session_storage.rb +20 -0
  163. data/lib/shopify_app/session/shop_session_storage.rb +42 -0
  164. data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +58 -0
  165. data/lib/shopify_app/session/user_session_storage.rb +42 -0
  166. data/lib/shopify_app/session/user_session_storage_with_scopes.rb +58 -0
  167. data/lib/shopify_app/test_helpers/all.rb +2 -0
  168. data/lib/shopify_app/test_helpers/webhook_verification_helper.rb +17 -0
  169. data/lib/shopify_app/utils.rb +24 -4
  170. data/lib/shopify_app/version.rb +2 -1
  171. data/lib/shopify_app.rb +65 -24
  172. data/package.json +27 -0
  173. data/service.yml +7 -0
  174. data/shipit.rubygems.yml +3 -0
  175. data/shopify_app.gemspec +20 -9
  176. data/translation.yml +7 -0
  177. data/webpack.config.js +24 -0
  178. data/yarn.lock +5215 -0
  179. metadata +274 -43
  180. data/.travis.yml +0 -17
  181. data/Gemfile.rails50 +0 -5
  182. data/Gemfile.ruby22 +0 -6
  183. data/Gemfile.ruby22.rails50 +0 -9
  184. data/ISSUE_TEMPLATE.md +0 -14
  185. data/QUICKSTART.md +0 -72
  186. data/RELEASING +0 -13
  187. data/lib/generators/shopify_app/home_controller/templates/shopify_app_ready_script.html.erb +0 -11
  188. data/lib/generators/shopify_app/install/templates/shopify_app.rb +0 -9
  189. data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -4
  190. data/lib/generators/shopify_app/install/templates/shopify_session_repository.rb +0 -23
  191. data/lib/generators/shopify_app/shop_model/templates/shopify_session_repository.rb +0 -7
  192. data/lib/shopify_app/in_memory_session_store.rb +0 -25
  193. data/lib/shopify_app/login_protection.rb +0 -103
  194. data/lib/shopify_app/scripttags_manager_job.rb +0 -15
  195. data/lib/shopify_app/session_storage.rb +0 -23
  196. data/lib/shopify_app/sessions_concern.rb +0 -101
  197. data/lib/shopify_app/shop.rb +0 -15
  198. data/lib/shopify_app/shopify_session_repository.rb +0 -34
  199. data/lib/shopify_app/webhook_verification.rb +0 -39
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ module Shopify
3
+ class AfterAuthenticateJob < ActiveJob::Base
4
+ def perform(shop_domain:)
5
+ shop = Shop.find_by(shopify_domain: shop_domain)
6
+
7
+ shop.with_shopify_session do
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+ require 'rails/generators/base'
3
+
4
+ module ShopifyApp
5
+ module Generators
6
+ class AddMarketingActivityExtensionGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def generate_app_extension
10
+ template("marketing_activities_controller.rb", "app/controllers/marketing_activities_controller.rb")
11
+ generate_routes
12
+ end
13
+
14
+ private
15
+
16
+ def generate_routes
17
+ inject_into_file(
18
+ 'config/routes.rb',
19
+ optimize_indentation(routes, 2),
20
+ after: "root :to => 'home#index'\n"
21
+ )
22
+ end
23
+
24
+ def routes
25
+ <<~EOS
26
+
27
+ resource :marketing_activities, only: [:create, :update] do
28
+ patch :resume
29
+ patch :pause
30
+ patch :delete
31
+ post :republish
32
+ post :preload_form_data
33
+ post :preview
34
+ post :errors
35
+ end
36
+ EOS
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MarketingActivitiesController < ShopifyApp::ExtensionVerificationController
4
+ def preload_form_data
5
+ preload_data = {
6
+ "form_data": {},
7
+ }
8
+ render(json: preload_data, status: :ok)
9
+ end
10
+
11
+ def update
12
+ render(json: {}, status: :accepted)
13
+ end
14
+
15
+ def pause
16
+ render(json: {}, status: :accepted)
17
+ end
18
+
19
+ def resume
20
+ render(json: {}, status: :accepted)
21
+ end
22
+
23
+ def delete
24
+ render(json: {}, status: :accepted)
25
+ end
26
+
27
+ def preview
28
+ placeholder_img = "https://cdn.shopify.com/s/files/1/0533/2089/files/placeholder-images-image_small.png"
29
+ preview_response = {
30
+ "desktop": {
31
+ "preview_url": placeholder_img,
32
+ "content_type": "text/html",
33
+ "width": 360,
34
+ "height": 200,
35
+ },
36
+ "mobile": {
37
+ "preview_url": placeholder_img,
38
+ "content_type": "text/html",
39
+ "width": 360,
40
+ "height": 200,
41
+ },
42
+ }
43
+ render(json: preview_response, status: :ok)
44
+ end
45
+
46
+ def create
47
+ render(json: {}, status: :ok)
48
+ end
49
+
50
+ def republish
51
+ render(json: {}, status: :accepted)
52
+ end
53
+
54
+ def errors
55
+ request_id = params[:request_id]
56
+ message = params[:message]
57
+
58
+ Rails.logger.info("[Marketing Activity App Error Feedback] Request id: #{request_id}, message: #{message}")
59
+
60
+ render(json: {}, status: :ok)
61
+ end
62
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
3
 
3
4
  module ShopifyApp
@@ -8,7 +9,7 @@ module ShopifyApp
8
9
  class_option :address, type: :string, aliases: "-a", required: true
9
10
 
10
11
  hook_for :test_framework, as: :job, in: :rails do |instance, generator|
11
- instance.invoke generator, [ instance.send(:job_file_name) ]
12
+ instance.invoke(generator, [instance.send(:job_file_name)])
12
13
  end
13
14
 
14
15
  def init_webhook_config
@@ -32,14 +33,14 @@ module ShopifyApp
32
33
  initializer = load_initializer
33
34
 
34
35
  unless initializer.include?(webhook_config)
35
- shell.say "Error adding webhook to config. Add this line manually: #{webhook_config}", :red
36
+ shell.say("Error adding webhook to config. Add this line manually: #{webhook_config}", :red)
36
37
  end
37
38
  end
38
39
 
39
40
  def add_webhook_job
40
41
  @job_file_name = job_file_name + '_job'
41
- @job_class_name = @job_file_name.classify
42
- template 'webhook_job.rb', "app/jobs/#{@job_file_name}.rb"
42
+ @job_class_name = @job_file_name.classify
43
+ template('webhook_job.rb', "app/jobs/#{@job_file_name}.rb")
43
44
  end
44
45
 
45
46
  private
@@ -2,6 +2,11 @@ class <%= @job_class_name %> < ActiveJob::Base
2
2
  def perform(shop_domain:, webhook:)
3
3
  shop = Shop.find_by(shopify_domain: shop_domain)
4
4
 
5
+ if shop.nil?
6
+ logger.error("#{self.class} failed: cannot find shop with domain '#{shop_domain}'")
7
+ return
8
+ end
9
+
5
10
  shop.with_shopify_session do
6
11
  end
7
12
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
3
 
3
4
  module ShopifyApp
@@ -6,11 +7,11 @@ module ShopifyApp
6
7
  source_root File.expand_path('../templates', __FILE__)
7
8
 
8
9
  def create_app_proxy_controller
9
- template 'app_proxy_controller.rb', 'app/controllers/app_proxy_controller.rb'
10
+ template('app_proxy_controller.rb', 'app/controllers/app_proxy_controller.rb')
10
11
  end
11
12
 
12
13
  def create_app_proxy_index_view
13
- copy_file 'index.html.erb', 'app/views/app_proxy/index.html.erb'
14
+ copy_file('index.html.erb', 'app/views/app_proxy/index.html.erb')
14
15
  end
15
16
 
16
17
  def add_app_proxy_route
@@ -18,7 +19,7 @@ module ShopifyApp
18
19
  'config/routes.rb',
19
20
  File.read(File.expand_path(find_in_source_paths('app_proxy_route.rb'))),
20
21
  after: "mount ShopifyApp::Engine, at: '/'\n"
21
- )
22
+ )
22
23
  end
23
24
  end
24
25
  end
@@ -1,8 +1,8 @@
1
+ # frozen_string_literal: true
1
2
  class AppProxyController < ApplicationController
2
- include ShopifyApp::AppProxyVerification
3
+ include ShopifyApp::AppProxyVerification
3
4
 
4
5
  def index
5
- render layout: false, content_type: 'application/liquid'
6
+ render(layout: false, content_type: 'application/liquid')
6
7
  end
7
-
8
8
  end
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
1
2
 
2
- namespace :app_proxy do
3
- root action: 'index'
4
- # simple routes without a specified controller will go to AppProxyController
5
-
6
- # more complex routes will go to controllers in the AppProxy namespace
7
- # resources :reviews
8
- # GET /app_proxy/reviews will now be routed to
9
- # AppProxy::ReviewsController#index, for example
10
- end
3
+ namespace :app_proxy do
4
+ root action: 'index'
5
+ # simple routes without a specified controller will go to AppProxyController
6
+
7
+ # more complex routes will go to controllers in the AppProxy namespace
8
+ # resources :reviews
9
+ # GET /app_proxy/reviews will now be routed to
10
+ # AppProxy::ReviewsController#index, for example
11
+ end
@@ -14,6 +14,6 @@ Your Shop Details:
14
14
  Shop URL : {{ shop.url }}
15
15
  Shop Currency : {{ shop.currency }}
16
16
  No. of products in this shop : {{ shop.products_count }}
17
- <br>
18
- <hr>
17
+ <br>
18
+ <hr>
19
19
  </pre>
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/base'
4
+
5
+ module ShopifyApp
6
+ module Generators
7
+ class AuthenticatedControllerGenerator < Rails::Generators::Base
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ def create_authenticated_controller
11
+ template('authenticated_controller.rb', 'app/controllers/authenticated_controller.rb')
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AuthenticatedController < ApplicationController
4
+ include ShopifyApp::Authenticated
5
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
3
 
3
4
  module ShopifyApp
@@ -7,7 +8,7 @@ module ShopifyApp
7
8
 
8
9
  def create_controllers
9
10
  controllers.each do |controller|
10
- copy_file controller
11
+ copy_file(controller)
11
12
  end
12
13
  end
13
14
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
3
 
3
4
  module ShopifyApp
@@ -5,27 +6,48 @@ module ShopifyApp
5
6
  class HomeControllerGenerator < Rails::Generators::Base
6
7
  source_root File.expand_path('../templates', __FILE__)
7
8
 
9
+ class_option :with_cookie_authentication, type: :boolean, default: false
10
+ class_option :embedded, type: :string, default: 'true'
11
+
8
12
  def create_home_controller
9
- template 'home_controller.rb', 'app/controllers/home_controller.rb'
13
+ template(home_controller_template, 'app/controllers/home_controller.rb')
14
+ end
15
+
16
+ def create_products_controller
17
+ generate("shopify_app:products_controller") unless with_cookie_authentication?
10
18
  end
11
19
 
12
20
  def create_home_index_view
13
- copy_file 'index.html.erb', 'app/views/home/index.html.erb'
14
- if embedded_app?
15
- prepend_to_file(
16
- 'app/views/home/index.html.erb',
17
- File.read(File.expand_path(find_in_source_paths('shopify_app_ready_script.html.erb')))
18
- )
19
- end
21
+ template('index.html.erb', 'app/views/home/index.html.erb')
20
22
  end
21
23
 
22
24
  def add_home_index_route
23
- route "root :to => 'home#index'"
25
+ route("root :to => 'home#index'")
26
+ end
27
+
28
+ private
29
+
30
+ def embedded?
31
+ options['embedded'] == 'true'
24
32
  end
25
33
 
26
34
  def embedded_app?
27
35
  ShopifyApp.configuration.embedded_app?
28
36
  end
37
+
38
+ def with_cookie_authentication?
39
+ options['with_cookie_authentication']
40
+ end
41
+
42
+ def home_controller_template
43
+ return 'unauthenticated_home_controller.rb' unless authenticated_home_controller_required?
44
+
45
+ 'home_controller.rb'
46
+ end
47
+
48
+ def authenticated_home_controller_required?
49
+ with_cookie_authentication? || !embedded? || !embedded_app?
50
+ end
29
51
  end
30
52
  end
31
53
  end
@@ -1,5 +1,10 @@
1
- class HomeController < ShopifyApp::AuthenticatedController
1
+ # frozen_string_literal: true
2
+
3
+ class HomeController < AuthenticatedController
4
+ include ShopifyApp::ShopAccessScopesVerification
5
+
2
6
  def index
3
7
  @products = ShopifyAPI::Product.find(:all, params: { limit: 10 })
8
+ @webhooks = ShopifyAPI::Webhook.find(:all)
4
9
  end
5
10
  end
@@ -1,7 +1,71 @@
1
- <h2>Products</h2>
1
+ <!DOCTYPE html>
2
+ <html lang="<%= I18n.locale %>">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <link
7
+ rel="stylesheet"
8
+ href="https://unpkg.com/@shopify/polaris@4.25.0/styles.min.css"
9
+ />
10
+ <% unless with_cookie_authentication? %> <script>
11
+ document.addEventListener("DOMContentLoaded", async function() {
12
+ var SessionToken = window["app-bridge"].actions.SessionToken
13
+ var app = window.app;
2
14
 
3
- <ul>
4
- <% @products.each do |product| %>
5
- <li><%= link_to product.title, "https://#{@shop_session.url}/admin/products/#{product.id}", target: "_top" %></li>
6
- <% end %>
7
- </ul>
15
+ app.dispatch(
16
+ SessionToken.request(),
17
+ );
18
+
19
+ // Save a session token for future requests
20
+ window.sessionToken = await new Promise((resolve) => {
21
+ app.subscribe(SessionToken.ActionType.RESPOND, (data) => {
22
+ resolve(data.sessionToken || "");
23
+ });
24
+ });
25
+
26
+ var fetchProducts = function() {
27
+ var headers = new Headers({ "Authorization": "Bearer " + window.sessionToken });
28
+ return fetch("/products", { headers })
29
+ .then(response => response.json())
30
+ .then(data => {
31
+ var products = data.products;
32
+
33
+ if (products === undefined || products.length == 0) {
34
+ document.getElementById("products").innerHTML = "<br>No products to display.";
35
+ } else {
36
+ var list = "";
37
+ products.forEach((product) => {
38
+ var link = `<a target="_top" href="https://<%%= @shop_origin %>/admin/products/${product.id}">`
39
+ list += "<li>" + link + product.title + "</a></li>";
40
+ });
41
+ document.getElementById("products").innerHTML = "<ul>" + list + "</ul>";
42
+ }
43
+ });
44
+ }();
45
+ });
46
+ </script>
47
+ <% end %> </head>
48
+ <body>
49
+ <h2>Products</h2>
50
+ <% unless with_cookie_authentication? %> <div id="products"><br>Loading...</div><% else %>
51
+ <ul>
52
+ <%% @products.each do |product| %>
53
+ <li><%%= link_to product.title, "https://#{@current_shopify_session.domain}/admin/products/#{product.id}", target: "_top" %></li>
54
+ <%% end %>
55
+ </ul>
56
+
57
+ <hr>
58
+ <% end %>
59
+ <h2>Webhooks</h2>
60
+
61
+ <%% if @webhooks.present? %>
62
+ <ul>
63
+ <%% @webhooks.each do |webhook| %>
64
+ <li><%%= webhook.topic %> : <%%= webhook.address %></li>
65
+ <%% end %>
66
+ </ul>
67
+ <%% else %>
68
+ <p>This app has not created any webhooks for this Shop. Add webhooks to your ShopifyApp initializer if you need webhooks</p>
69
+ <%% end %>
70
+ </body>
71
+ </html>
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HomeController < ApplicationController
4
+ include ShopifyApp::EmbeddedApp
5
+ include ShopifyApp::RequireKnownShop
6
+ include ShopifyApp::ShopAccessScopesVerification
7
+
8
+ def index
9
+ @shop_origin = current_shopify_domain
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
+ # frozen_string_literal: true
1
2
  require 'rails/generators/base'
2
- require 'rails/generators/active_record'
3
3
 
4
4
  module ShopifyApp
5
5
  module Generators
@@ -7,60 +7,111 @@ module ShopifyApp
7
7
  include Rails::Generators::Migration
8
8
  source_root File.expand_path('../templates', __FILE__)
9
9
 
10
- class_option :application_name, type: :string
11
- class_option :api_key, type: :string, default: '<api_key>'
12
- class_option :secret, type: :string, default: '<secret>'
13
- class_option :scope, type: :string, default: 'read_orders, read_products'
10
+ class_option :application_name, type: :array, default: ['My', 'Shopify', 'App']
11
+ class_option :scope, type: :array, default: ['read_products']
14
12
  class_option :embedded, type: :string, default: 'true'
13
+ class_option :api_version, type: :string, default: nil
14
+ class_option :with_cookie_authentication, type: :boolean, default: false
15
15
 
16
16
  def create_shopify_app_initializer
17
- @application_name = options['application_name']
18
- @api_key = options['api_key']
19
- @secret = options['secret']
20
- @scope = options['scope']
17
+ @application_name = format_array_argument(options['application_name'])
18
+ @scope = format_array_argument(options['scope'])
19
+ @api_version = options['api_version'] || ShopifyAPI::Meta.admin_versions.find(&:latest_supported).handle
21
20
 
22
- template 'shopify_app.rb', 'config/initializers/shopify_app.rb'
21
+ template('shopify_app.rb', 'config/initializers/shopify_app.rb')
22
+ end
23
+
24
+ def create_session_store_initializer
25
+ copy_file('session_store.rb', 'config/initializers/session_store.rb')
23
26
  end
24
27
 
25
28
  def create_and_inject_into_omniauth_initializer
26
- unless File.exist? "config/initializers/omniauth.rb"
27
- copy_file 'omniauth.rb', 'config/initializers/omniauth.rb'
29
+ unless File.exist?("config/initializers/omniauth.rb")
30
+ copy_file('omniauth.rb', 'config/initializers/omniauth.rb')
28
31
  end
29
32
 
33
+ return if !Rails.env.test? && shopify_provider_exists?
34
+
30
35
  inject_into_file(
31
36
  'config/initializers/omniauth.rb',
32
- File.read(File.expand_path(find_in_source_paths('shopify_provider.rb'))),
33
- after: "Rails.application.config.middleware.use OmniAuth::Builder do\n"
37
+ shopify_provider_template,
38
+ after: "Rails.application.config.middleware.use(OmniAuth::Builder) do\n"
34
39
  )
35
40
  end
36
41
 
37
- def create_shopify_session_repository_initializer
38
- copy_file 'shopify_session_repository.rb', 'config/initializers/shopify_session_repository.rb'
39
- end
42
+ def create_embedded_app_layout
43
+ return unless embedded_app?
44
+
45
+ copy_file('embedded_app.html.erb', 'app/views/layouts/embedded_app.html.erb')
46
+ copy_file('_flash_messages.html.erb', 'app/views/layouts/_flash_messages.html.erb')
40
47
 
41
- def inject_embedded_app_options_to_application
42
- if embedded_app?
43
- application "config.action_dispatch.default_headers.delete('X-Frame-Options')"
44
- application "config.action_dispatch.default_headers['P3P'] = 'CP=\"Not used\"'"
48
+ if ShopifyApp.use_webpacker?
49
+ copy_file('shopify_app.js', 'app/javascript/shopify_app/shopify_app.js')
50
+ copy_file('flash_messages.js', 'app/javascript/shopify_app/flash_messages.js')
51
+ copy_file('shopify_app_index.js', 'app/javascript/shopify_app/index.js')
52
+ append_to_file('app/javascript/packs/application.js', "require(\"shopify_app\")\n")
53
+ else
54
+ copy_file('shopify_app.js', 'app/assets/javascripts/shopify_app.js')
55
+ copy_file('flash_messages.js', 'app/assets/javascripts/flash_messages.js')
45
56
  end
46
57
  end
47
58
 
48
- def create_embedded_app_layout
49
- if embedded_app?
50
- copy_file 'embedded_app.html.erb', 'app/views/layouts/embedded_app.html.erb'
51
- copy_file '_flash_messages.html.erb', 'app/views/layouts/_flash_messages.html.erb'
52
- end
59
+ def create_user_agent_initializer
60
+ template('user_agent.rb', 'config/initializers/user_agent.rb')
53
61
  end
54
62
 
55
63
  def mount_engine
56
- route "mount ShopifyApp::Engine, at: '/'"
64
+ route("mount ShopifyApp::Engine, at: '/'")
65
+ end
66
+
67
+ def insert_hosts_into_development_config
68
+ inject_into_file(
69
+ 'config/environments/development.rb',
70
+ " config.hosts = (config.hosts rescue []) << /\\w+\\.ngrok\\.io/\n",
71
+ after: "Rails.application.configure do\n"
72
+ )
57
73
  end
58
74
 
59
75
  private
60
76
 
77
+ def shopify_provider_exists?
78
+ File.open("config/initializers/omniauth.rb") do |file|
79
+ file.each_line do |line|
80
+ if line =~ /provider :shopify/
81
+ puts "\e[33m#{omniauth_warning}\e[0m"
82
+ return true
83
+ end
84
+ end
85
+ end
86
+ false
87
+ end
88
+
89
+ def omniauth_warning
90
+ <<~OMNIAUTH
91
+ \n[WARNING] The Shopify App generator attempted to add the following Shopify Omniauth \
92
+ provider 'config/initializers/omniauth.rb':
93
+
94
+ \e[0m#{shopify_provider_template}\e[33m
95
+
96
+ Consider updating 'config/initializers/omniauth.rb' to match the configuration above.
97
+ OMNIAUTH
98
+ end
99
+
100
+ def shopify_provider_template
101
+ File.read(File.expand_path(find_in_source_paths('shopify_provider.rb.tt')))
102
+ end
103
+
61
104
  def embedded_app?
62
105
  options['embedded'] == 'true'
63
106
  end
107
+
108
+ def format_array_argument(array)
109
+ array.join(' ').tr('"', '')
110
+ end
111
+
112
+ def with_cookie_authentication?
113
+ options['with_cookie_authentication'] || !embedded_app?
114
+ end
64
115
  end
65
116
  end
66
117
  end
@@ -1,15 +1,3 @@
1
1
  <% content_for :javascript do %>
2
- <script type="text/javascript">
3
- var eventName = typeof(Turbolinks) !== 'undefined' ? 'page:change' : 'DOMContentLoaded';
4
-
5
- document.addEventListener(eventName, function() {
6
- <% if flash[:notice] %>
7
- ShopifyApp.flashNotice("<%= j flash[:notice].html_safe %>");
8
- <% end %>
9
-
10
- <% if flash[:error] %>
11
- ShopifyApp.flashError("<%= j flash[:error].html_safe %>");
12
- <% end %>
13
- });
14
- </script>
2
+ <%= content_tag(:div, nil, id: 'shopify-app-flash', data: { flash: { notice: flash[:notice], error: flash[:error] } }) %>
15
3
  <% end %>
@@ -3,9 +3,13 @@
3
3
  <head>
4
4
  <meta charset="utf-8" />
5
5
  <% application_name = ShopifyApp.configuration.application_name %>
6
- <title><%= application_name.presence || 'Embedded Shopify App Example' %></title>
6
+ <title><%= application_name %></title>
7
7
  <%= stylesheet_link_tag 'application' %>
8
- <%= javascript_include_tag 'application', "data-turbolinks-track" => true %>
8
+ <% if ShopifyApp.use_webpacker? %>
9
+ <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
10
+ <% else %>
11
+ <%= javascript_include_tag 'application', "data-turbolinks-track" => true %>
12
+ <% end %>
9
13
  <%= csrf_meta_tags %>
10
14
  </head>
11
15
 
@@ -20,16 +24,13 @@
20
24
 
21
25
  <%= render 'layouts/flash_messages' %>
22
26
 
23
- <script src="https://cdn.shopify.com/s/assets/external/app.js?<%= Time.now.strftime('%Y%m%d%H') %>"></script>
27
+ <script src="https://unpkg.com/@shopify/app-bridge"></script>
24
28
 
25
- <script type="text/javascript">
26
- ShopifyApp.init({
27
- apiKey: "<%= ShopifyApp.configuration.api_key %>",
28
- shopOrigin: "<%= "https://#{ @shop_session.url }" if @shop_session %>",
29
- debug: <%= Rails.env.development? ? 'true' : 'false' %>,
30
- forceRedirect: true
31
- });
32
- </script>
29
+ <%= content_tag(:div, nil, id: 'shopify-app-init', data: {
30
+ api_key: ShopifyApp.configuration.api_key,
31
+ shop_origin: @shop_origin || (@current_shopify_session.domain if @current_shopify_session),
32
+ debug: Rails.env.development?
33
+ } ) %>
33
34
 
34
35
  <% if content_for?(:javascript) %>
35
36
  <div id="ContentForJavascript" data-turbolinks-temporary>
@@ -0,0 +1,24 @@
1
+ var eventName = typeof(Turbolinks) !== 'undefined' ? 'turbolinks:load' : 'DOMContentLoaded';
2
+
3
+ if (!document.documentElement.hasAttribute("data-turbolinks-preview")) {
4
+ document.addEventListener(eventName, function flash() {
5
+ var flashData = JSON.parse(document.getElementById('shopify-app-flash').dataset.flash);
6
+
7
+ var Toast = window['app-bridge'].actions.Toast;
8
+
9
+ if (flashData.notice) {
10
+ Toast.create(app, {
11
+ message: flashData.notice,
12
+ duration: 5000,
13
+ }).dispatch(Toast.Action.SHOW);
14
+ }
15
+
16
+ if (flashData.error) {
17
+ Toast.create(app, {
18
+ message: flashData.error,
19
+ duration: 5000,
20
+ isError: true,
21
+ }).dispatch(Toast.Action.SHOW);
22
+ }
23
+ });
24
+ }