shopify_app 13.2.0 → 20.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE/bug-report.md +63 -0
  4. data/.github/ISSUE_TEMPLATE/config.yml +1 -0
  5. data/.github/ISSUE_TEMPLATE/feature-request.md +33 -0
  6. data/.github/PULL_REQUEST_TEMPLATE.md +22 -0
  7. data/.github/workflows/build.yml +40 -0
  8. data/.github/workflows/cla.yml +22 -0
  9. data/.github/workflows/close-waiting-for-response-issues.yml +20 -0
  10. data/.github/workflows/release.yml +24 -0
  11. data/.github/workflows/remove-labels-on-activity.yml +16 -0
  12. data/.github/workflows/rubocop.yml +22 -0
  13. data/.github/workflows/stale.yml +31 -0
  14. data/.gitignore +1 -2
  15. data/.nvmrc +1 -1
  16. data/.rubocop.yml +2 -0
  17. data/.ruby-version +1 -1
  18. data/CHANGELOG.md +221 -0
  19. data/CONTRIBUTING.md +81 -0
  20. data/Gemfile +5 -2
  21. data/Gemfile.lock +248 -0
  22. data/README.md +74 -563
  23. data/Rakefile +4 -3
  24. data/SECURITY.md +59 -0
  25. data/app/assets/images/storage_access.svg +1 -2
  26. data/app/assets/javascripts/shopify_app/app_bridge_3.1.1.js +10 -0
  27. data/app/assets/javascripts/shopify_app/app_bridge_redirect.js +22 -0
  28. data/app/assets/javascripts/shopify_app/app_bridge_utils_3.1.1.js +1 -0
  29. data/app/assets/javascripts/shopify_app/post_redirect.js +9 -0
  30. data/app/assets/javascripts/shopify_app/redirect.js +10 -14
  31. data/app/assets/javascripts/shopify_app/storage_access.js +5 -10
  32. data/app/assets/javascripts/shopify_app/top_level_interaction.js +1 -1
  33. data/app/controllers/concerns/shopify_app/authenticated.rb +4 -0
  34. data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +39 -0
  35. data/app/controllers/concerns/shopify_app/require_known_shop.rb +48 -0
  36. data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +40 -0
  37. data/app/controllers/shopify_app/authenticated_controller.rb +1 -0
  38. data/app/controllers/shopify_app/callback_controller.rb +56 -77
  39. data/app/controllers/shopify_app/extension_verification_controller.rb +2 -7
  40. data/app/controllers/shopify_app/sessions_controller.rb +33 -117
  41. data/app/controllers/shopify_app/webhooks_controller.rb +5 -26
  42. data/app/views/shopify_app/partials/_button_styles.html.erb +41 -36
  43. data/app/views/shopify_app/partials/_card_styles.html.erb +3 -3
  44. data/app/views/shopify_app/partials/_empty_state_styles.html.erb +28 -59
  45. data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
  46. data/app/views/shopify_app/partials/_layout_styles.html.erb +16 -1
  47. data/app/views/shopify_app/partials/_typography_styles.html.erb +6 -6
  48. data/app/views/shopify_app/sessions/enable_cookies.html.erb +2 -7
  49. data/app/views/shopify_app/sessions/new.html.erb +38 -110
  50. data/app/views/shopify_app/sessions/request_storage_access.html.erb +12 -12
  51. data/app/views/shopify_app/sessions/top_level_interaction.html.erb +21 -22
  52. data/app/views/shopify_app/shared/post_redirect_to_auth_shopify.html.erb +13 -0
  53. data/app/views/shopify_app/shared/redirect.html.erb +2 -2
  54. data/config/locales/de.yml +11 -11
  55. data/config/locales/ja.yml +4 -4
  56. data/config/locales/nl.yml +2 -2
  57. data/config/locales/th.yml +4 -4
  58. data/config/locales/vi.yml +22 -0
  59. data/config/locales/zh-CN.yml +2 -2
  60. data/config/routes.rb +20 -12
  61. data/docs/Quickstart.md +19 -83
  62. data/docs/Releasing.md +18 -15
  63. data/docs/Troubleshooting.md +140 -5
  64. data/docs/Upgrading.md +247 -0
  65. data/docs/shopify_app/authentication.md +128 -0
  66. data/docs/shopify_app/content-security-policy.md +10 -0
  67. data/docs/shopify_app/engine.md +82 -0
  68. data/docs/shopify_app/generators.md +127 -0
  69. data/docs/shopify_app/handling-access-scopes-changes.md +24 -0
  70. data/docs/shopify_app/script-tags.md +28 -0
  71. data/docs/shopify_app/session-repository.md +88 -0
  72. data/docs/shopify_app/testing.md +38 -0
  73. data/docs/shopify_app/webhooks.md +72 -0
  74. data/karma.conf.js +1 -1
  75. data/lib/generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator.rb +10 -9
  76. data/lib/generators/shopify_app/add_after_authenticate_job/templates/after_authenticate_job.rb +1 -0
  77. data/lib/generators/shopify_app/add_marketing_activity_extension/add_marketing_activity_extension_generator.rb +4 -3
  78. data/lib/generators/shopify_app/add_webhook/add_webhook_generator.rb +15 -14
  79. data/lib/generators/shopify_app/add_webhook/templates/webhook_job.rb.tt +9 -1
  80. data/lib/generators/shopify_app/app_proxy_controller/app_proxy_controller_generator.rb +7 -6
  81. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_controller.rb +2 -1
  82. data/lib/generators/shopify_app/app_proxy_controller/templates/app_proxy_route.rb +1 -1
  83. data/lib/generators/shopify_app/authenticated_controller/authenticated_controller_generator.rb +4 -4
  84. data/lib/generators/shopify_app/controllers/controllers_generator.rb +5 -4
  85. data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +27 -4
  86. data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +12 -2
  87. data/lib/generators/shopify_app/home_controller/templates/index.html.erb +74 -16
  88. data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +16 -0
  89. data/lib/generators/shopify_app/install/install_generator.rb +52 -40
  90. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +5 -2
  91. data/lib/generators/shopify_app/install/templates/flash_messages.js +0 -2
  92. data/lib/generators/shopify_app/install/templates/session_store.rb +2 -1
  93. data/lib/generators/shopify_app/install/templates/shopify_app.js +1 -1
  94. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +43 -5
  95. data/lib/generators/shopify_app/install/templates/shopify_app_importmap.js +13 -0
  96. data/lib/generators/shopify_app/products_controller/products_controller_generator.rb +19 -0
  97. data/lib/generators/shopify_app/products_controller/templates/products_controller.rb +8 -0
  98. data/lib/generators/shopify_app/rotate_shopify_token_job/rotate_shopify_token_job_generator.rb +4 -4
  99. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token.rake +1 -0
  100. data/lib/generators/shopify_app/rotate_shopify_token_job/templates/rotate_shopify_token_job.rb +1 -1
  101. data/lib/generators/shopify_app/routes/routes_generator.rb +6 -5
  102. data/lib/generators/shopify_app/routes/templates/routes.rb +5 -5
  103. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +35 -7
  104. data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
  105. data/lib/generators/shopify_app/shop_model/templates/shop.rb +2 -1
  106. data/lib/generators/shopify_app/shopify_app_generator.rb +4 -3
  107. data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
  108. data/lib/generators/shopify_app/user_model/templates/user.rb +2 -1
  109. data/lib/generators/shopify_app/user_model/user_model_generator.rb +35 -7
  110. data/lib/generators/shopify_app/views/views_generator.rb +5 -4
  111. data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
  112. data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
  113. data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
  114. data/lib/shopify_app/configuration.rb +58 -11
  115. data/lib/shopify_app/controller_concerns/app_proxy_verification.rb +4 -4
  116. data/lib/shopify_app/controller_concerns/csrf_protection.rb +16 -0
  117. data/lib/shopify_app/controller_concerns/embedded_app.rb +6 -3
  118. data/lib/shopify_app/controller_concerns/ensure_billing.rb +243 -0
  119. data/lib/shopify_app/controller_concerns/frame_ancestors.rb +16 -0
  120. data/lib/shopify_app/controller_concerns/itp.rb +3 -3
  121. data/lib/shopify_app/controller_concerns/localization.rb +1 -0
  122. data/lib/shopify_app/controller_concerns/login_protection.rb +105 -90
  123. data/lib/shopify_app/controller_concerns/payload_verification.rb +25 -0
  124. data/lib/shopify_app/controller_concerns/redirect_for_embedded.rb +36 -0
  125. data/lib/shopify_app/controller_concerns/sanitized_params.rb +36 -0
  126. data/lib/shopify_app/controller_concerns/webhook_verification.rb +3 -18
  127. data/lib/shopify_app/engine.rb +26 -11
  128. data/lib/shopify_app/errors.rb +34 -0
  129. data/lib/shopify_app/jobs/scripttags_manager_job.rb +2 -2
  130. data/lib/shopify_app/jobs/webhooks_manager_job.rb +4 -5
  131. data/lib/shopify_app/managers/scripttags_manager.rb +12 -6
  132. data/lib/shopify_app/managers/webhooks_manager.rb +62 -42
  133. data/lib/shopify_app/middleware/jwt_middleware.rb +6 -3
  134. data/lib/shopify_app/session/in_memory_session_store.rb +2 -3
  135. data/lib/shopify_app/session/in_memory_shop_session_store.rb +10 -7
  136. data/lib/shopify_app/session/in_memory_user_session_store.rb +10 -7
  137. data/lib/shopify_app/session/jwt.rb +19 -16
  138. data/lib/shopify_app/session/null_user_session_store.rb +2 -1
  139. data/lib/shopify_app/session/session_repository.rb +40 -2
  140. data/lib/shopify_app/session/session_storage.rb +4 -6
  141. data/lib/shopify_app/session/shop_session_storage.rb +6 -6
  142. data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +57 -0
  143. data/lib/shopify_app/session/user_session_storage.rb +20 -7
  144. data/lib/shopify_app/session/user_session_storage_with_scopes.rb +71 -0
  145. data/lib/shopify_app/test_helpers/all.rb +2 -1
  146. data/lib/shopify_app/test_helpers/webhook_verification_helper.rb +4 -3
  147. data/lib/shopify_app/utils.rb +14 -7
  148. data/lib/shopify_app/version.rb +2 -1
  149. data/lib/shopify_app.rb +52 -29
  150. data/package.json +7 -8
  151. data/service.yml +1 -5
  152. data/shopify_app.gemspec +22 -20
  153. data/translation.yml +1 -1
  154. data/yarn.lock +2173 -2206
  155. metadata +110 -56
  156. data/.github/ISSUE_TEMPLATE.md +0 -14
  157. data/.github/probots.yml +0 -2
  158. data/.travis.yml +0 -28
  159. data/config/locales/hi.yml +0 -23
  160. data/config/locales/ms.yml +0 -22
  161. data/docs/install-on-dev-shop.png +0 -0
  162. data/docs/test-your-app.png +0 -0
  163. data/lib/generators/shopify_app/install/templates/omniauth.rb +0 -3
  164. data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -20
  165. data/lib/generators/shopify_app/install/templates/user_agent.rb +0 -6
  166. data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +0 -34
  167. data/package-lock.json +0 -7245
@@ -1,21 +1,79 @@
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
+ <% if embedded_app? %>
11
+ <script type="module">
12
+ async function displayProducts() {
13
+ var SessionToken = window["app-bridge"].actions.SessionToken
14
+ var app = window.app;
2
15
 
3
- <ul>
4
- <% @products.each do |product| %>
5
- <li><%= link_to product.title, "https://#{@current_shopify_session.domain}/admin/products/#{product.id}", target: "_top" %></li>
6
- <% end %>
7
- </ul>
16
+ app.dispatch(
17
+ SessionToken.request(),
18
+ );
19
+ // Save a session token for future requests
20
+ window.sessionToken = await new Promise((resolve) => {
21
+ app.subscribe(SessionToken.Action.RESPOND, (data) => {
22
+ resolve(data.sessionToken || "");
23
+ });
24
+ });
8
25
 
9
- <hr>
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;
10
32
 
11
- <h2>Webhooks</h2>
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
+ };
12
46
 
13
- <% if @webhooks.present? %>
14
- <ul>
15
- <% @webhooks.each do |webhook| %>
16
- <li><%= webhook.topic %> : <%= webhook.address %></li>
47
+ <% if ShopifyApp.use_importmap? %>
48
+ import "lib/shopify_app"
49
+ displayProducts();
50
+ <% else %>
51
+ document.addEventListener("DOMContentLoaded", displayProducts);
52
+ <% end %>
53
+ </script>
17
54
  <% end %>
18
- </ul>
19
- <% else %>
20
- <p>This app has not created any webhooks for this Shop. Add webhooks to your ShopifyApp initializer if you need webhooks</p>
21
- <% end %>
55
+ </head>
56
+ <body>
57
+ <h2>Products</h2>
58
+ <% if embedded_app? %> <div id="products"><br>Loading...</div><% else %>
59
+ <ul>
60
+ <%% @products.each do |product| %>
61
+ <li><%%= link_to product.title, "https://#{@current_shopify_session.shop}/admin/products/#{product.id}", target: "_top" %></li>
62
+ <%% end %>
63
+ </ul>
64
+
65
+ <hr>
66
+ <% end %>
67
+ <h2>Webhooks</h2>
68
+
69
+ <%% if @webhooks.present? %>
70
+ <ul>
71
+ <%% @webhooks.each do |webhook| %>
72
+ <li><%%= webhook.topic %> : <%%= webhook.address %></li>
73
+ <%% end %>
74
+ </ul>
75
+ <%% else %>
76
+ <p>This app has not created any webhooks for this Shop. Add webhooks to your ShopifyApp initializer if you need webhooks</p>
77
+ <%% end %>
78
+ </body>
79
+ </html>
@@ -0,0 +1,16 @@
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
+ if ShopifyAPI::Context.embedded? && (!params[:embedded].present? || params[:embedded] != "1")
10
+ redirect_to(ShopifyAPI::Auth.embedded_app_url(params[:host]) + request.path, allow_other_host: true)
11
+ else
12
+ @shop_origin = current_shopify_domain
13
+ @host = params[:host]
14
+ end
15
+ end
16
+ end
@@ -1,82 +1,94 @@
1
1
  # frozen_string_literal: true
2
- require 'rails/generators/base'
2
+
3
+ require "rails/generators/base"
3
4
 
4
5
  module ShopifyApp
5
6
  module Generators
6
7
  class InstallGenerator < Rails::Generators::Base
7
8
  include Rails::Generators::Migration
8
- source_root File.expand_path('../templates', __FILE__)
9
+ source_root File.expand_path("../templates", __FILE__)
9
10
 
10
- class_option :application_name, type: :array, default: ['My', 'Shopify', 'App']
11
- class_option :scope, type: :array, default: ['read_products']
12
- class_option :embedded, type: :string, default: 'true'
11
+ class_option :application_name, type: :array, default: ["My", "Shopify", "App"]
12
+ class_option :scope, type: :array, default: ["read_products"]
13
+ class_option :embedded, type: :string, default: "true"
13
14
  class_option :api_version, type: :string, default: nil
14
15
 
16
+ NGROK_HOST = "/\[-\\w]+\\.ngrok\\.io/"
17
+ CLOUDFLARE_HOST = "/\[-\\w]+\\.trycloudflare\\.com/"
18
+
15
19
  def create_shopify_app_initializer
16
- @application_name = format_array_argument(options['application_name'])
17
- @scope = format_array_argument(options['scope'])
18
- @api_version = options['api_version'] || ShopifyAPI::Meta.admin_versions.find(&:latest_supported).handle
20
+ @application_name = format_array_argument(options["application_name"])
21
+ @scope = format_array_argument(options["scope"])
22
+ @api_version = options["api_version"] || ShopifyAPI::LATEST_SUPPORTED_ADMIN_VERSION
19
23
 
20
- template('shopify_app.rb', 'config/initializers/shopify_app.rb')
24
+ template("shopify_app.rb", "config/initializers/shopify_app.rb")
21
25
  end
22
26
 
23
27
  def create_session_store_initializer
24
- copy_file('session_store.rb', 'config/initializers/session_store.rb')
25
- end
26
-
27
- def create_and_inject_into_omniauth_initializer
28
- unless File.exist?("config/initializers/omniauth.rb")
29
- copy_file('omniauth.rb', 'config/initializers/omniauth.rb')
30
- end
31
-
32
- inject_into_file(
33
- 'config/initializers/omniauth.rb',
34
- File.read(File.expand_path(find_in_source_paths('shopify_provider.rb'))),
35
- after: "Rails.application.config.middleware.use(OmniAuth::Builder) do\n"
36
- )
28
+ copy_file("session_store.rb", "config/initializers/session_store.rb")
37
29
  end
38
30
 
39
31
  def create_embedded_app_layout
40
32
  return unless embedded_app?
41
33
 
42
- copy_file('embedded_app.html.erb', 'app/views/layouts/embedded_app.html.erb')
43
- copy_file('_flash_messages.html.erb', 'app/views/layouts/_flash_messages.html.erb')
34
+ copy_file("embedded_app.html.erb", "app/views/layouts/embedded_app.html.erb")
35
+ copy_file("_flash_messages.html.erb", "app/views/layouts/_flash_messages.html.erb")
44
36
 
45
37
  if ShopifyApp.use_webpacker?
46
- copy_file('shopify_app.js', 'app/javascript/shopify_app/shopify_app.js')
47
- copy_file('flash_messages.js', 'app/javascript/shopify_app/flash_messages.js')
48
- copy_file('shopify_app_index.js', 'app/javascript/shopify_app/index.js')
49
- append_to_file('app/javascript/packs/application.js', "require(\"shopify_app\")\n")
38
+ copy_file("shopify_app.js", "app/javascript/shopify_app/shopify_app.js")
39
+ copy_file("flash_messages.js", "app/javascript/shopify_app/flash_messages.js")
40
+ copy_file("shopify_app_index.js", "app/javascript/shopify_app/index.js")
41
+ append_to_file("app/javascript/packs/application.js", "require(\"shopify_app\")\n")
42
+ elsif ShopifyApp.use_importmap?
43
+ copy_file("shopify_app_importmap.js", "app/javascript/lib/shopify_app.js")
44
+ copy_file("flash_messages.js", "app/javascript/lib/flash_messages.js")
45
+ append_to_file("config/importmap.rb", "pin_all_from \"app/javascript/lib\", under: \"lib\"\n")
50
46
  else
51
- copy_file('shopify_app.js', 'app/assets/javascripts/shopify_app.js')
52
- copy_file('flash_messages.js', 'app/assets/javascripts/flash_messages.js')
47
+ copy_file("shopify_app.js", "app/assets/javascripts/shopify_app.js")
48
+ copy_file("flash_messages.js", "app/assets/javascripts/flash_messages.js")
53
49
  end
54
50
  end
55
51
 
56
- def create_user_agent_initializer
57
- template('user_agent.rb', 'config/initializers/user_agent.rb')
58
- end
59
-
60
52
  def mount_engine
61
53
  route("mount ShopifyApp::Engine, at: '/'")
62
54
  end
63
55
 
64
56
  def insert_hosts_into_development_config
57
+ "Rails.application.configure do\n"
58
+ .then { insert_tunnel_host_rules("ngrok", _1, NGROK_HOST + "\n") }
59
+ .then { insert_tunnel_host_rules("Cloudflare", _1, CLOUDFLARE_HOST + "\n") }
60
+ end
61
+
62
+ private
63
+
64
+ def add_comment_explaining_tunnel_host(provider_name, insert_after_line)
65
+ comment = " # Allow #{provider_name} tunnels for secure Shopify OAuth redirects\n"
65
66
  inject_into_file(
66
- 'config/environments/development.rb',
67
- " config.hosts = (config.hosts rescue []) << /\\h+.ngrok.io/\n",
68
- after: "Rails.application.configure do\n"
67
+ "config/environments/development.rb",
68
+ comment,
69
+ after: insert_after_line
69
70
  )
71
+ comment
70
72
  end
71
73
 
72
- private
74
+ def insert_tunnel_host_rules(provider_name, insert_after_line, provider_host_domain)
75
+ explaination_comment = add_comment_explaining_tunnel_host(provider_name, insert_after_line)
76
+ host_line = " config.hosts = (config.hosts rescue []) << #{provider_host_domain}"
77
+
78
+ inject_into_file(
79
+ "config/environments/development.rb",
80
+ host_line,
81
+ after: explaination_comment
82
+ )
83
+ host_line
84
+ end
73
85
 
74
86
  def embedded_app?
75
- options['embedded'] == 'true'
87
+ options["embedded"] == "true"
76
88
  end
77
89
 
78
90
  def format_array_argument(array)
79
- array.join(' ').tr('"', '')
91
+ array.join(" ").tr('"', "")
80
92
  end
81
93
  end
82
94
  end
@@ -7,6 +7,8 @@
7
7
  <%= stylesheet_link_tag 'application' %>
8
8
  <% if ShopifyApp.use_webpacker? %>
9
9
  <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
10
+ <% elsif ShopifyApp.use_importmap? %>
11
+ <%= javascript_importmap_tags %>
10
12
  <% else %>
11
13
  <%= javascript_include_tag 'application', "data-turbolinks-track" => true %>
12
14
  <% end %>
@@ -24,11 +26,12 @@
24
26
 
25
27
  <%= render 'layouts/flash_messages' %>
26
28
 
27
- <script src="https://unpkg.com/@shopify/app-bridge"></script>
29
+ <script src="https://unpkg.com/@shopify/app-bridge@2"></script>
28
30
 
29
31
  <%= content_tag(:div, nil, id: 'shopify-app-init', data: {
30
32
  api_key: ShopifyApp.configuration.api_key,
31
- shop_origin: (@current_shopify_session.domain if @current_shopify_session),
33
+ shop_origin: @shop_origin || (@current_shopify_session.shop if @current_shopify_session),
34
+ host: @host,
32
35
  debug: Rails.env.development?
33
36
  } ) %>
34
37
 
@@ -20,7 +20,5 @@ if (!document.documentElement.hasAttribute("data-turbolinks-preview")) {
20
20
  isError: true,
21
21
  }).dispatch(Toast.Action.SHOW);
22
22
  }
23
-
24
- document.removeEventListener(eventName, flash)
25
23
  });
26
24
  }
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # Be sure to restart your server when you modify this file.
3
4
 
4
- Rails.application.config.session_store(:cookie_store, key: '_example_session', expire_after: 14.days)
5
+ Rails.application.config.session_store(:cookie_store, key: "_example_session", expire_after: 14.days)
@@ -4,7 +4,7 @@ document.addEventListener('DOMContentLoaded', () => {
4
4
  var createApp = AppBridge.default;
5
5
  window.app = createApp({
6
6
  apiKey: data.apiKey,
7
- shopOrigin: data.shopOrigin,
7
+ host: data.host,
8
8
  });
9
9
 
10
10
  var actions = AppBridge.actions;
@@ -1,15 +1,53 @@
1
1
  ShopifyApp.configure do |config|
2
2
  config.application_name = "<%= @application_name %>"
3
- config.api_key = ENV['SHOPIFY_API_KEY']
4
- config.secret = ENV['SHOPIFY_API_SECRET']
5
3
  config.old_secret = "<%= @old_secret %>"
6
4
  config.scope = "<%= @scope %>" # Consult this page for more scope options:
7
- # https://help.shopify.com/en/api/getting-started/authentication/oauth/scopes
5
+ # https://help.shopify.com/en/api/getting-started/authentication/oauth/scopes
8
6
  config.embedded_app = <%= embedded_app? %>
9
7
  config.after_authenticate_job = false
10
8
  config.api_version = "<%= @api_version %>"
11
9
  config.shop_session_repository = 'Shop'
10
+
11
+ config.reauth_on_access_scope_changes = true
12
+
13
+ config.api_key = ENV.fetch('SHOPIFY_API_KEY', '').presence
14
+ config.secret = ENV.fetch('SHOPIFY_API_SECRET', '').presence
15
+
16
+ # You may want to charge merchants for using your app. Setting the billing configuration will cause the Authenticated
17
+ # controller concern to check that the session is for a merchant that has an active one-time payment or subscription.
18
+ # If no payment is found, it starts off the process and sends the merchant to a confirmation URL so that they can
19
+ # approve the purchase.
20
+ #
21
+ # Learn more about billing in our documentation: https://shopify.dev/apps/billing
22
+ # config.billing = ShopifyApp::BillingConfiguration.new(
23
+ # charge_name: "My app billing charge",
24
+ # amount: 5,
25
+ # interval: ShopifyApp::BillingConfiguration::INTERVAL_EVERY_30_DAYS,
26
+ # currency_code: "USD", # Only supports USD for now
27
+ # )
28
+
29
+ if defined? Rails::Server
30
+ raise('Missing SHOPIFY_API_KEY. See https://github.com/Shopify/shopify_app#requirements') unless config.api_key
31
+ raise('Missing SHOPIFY_API_SECRET. See https://github.com/Shopify/shopify_app#requirements') unless config.secret
32
+ end
12
33
  end
13
34
 
14
- # ShopifyApp::Utils.fetch_known_api_versions # Uncomment to fetch known api versions from shopify servers on boot
15
- # ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown # Uncomment to raise an error if attempting to use an api version that was not previously known
35
+ Rails.application.config.after_initialize do
36
+ if ShopifyApp.configuration.api_key.present? && ShopifyApp.configuration.secret.present?
37
+ ShopifyAPI::Context.setup(
38
+ api_key: ShopifyApp.configuration.api_key,
39
+ api_secret_key: ShopifyApp.configuration.secret,
40
+ api_version: ShopifyApp.configuration.api_version,
41
+ host_name: URI(ENV.fetch('HOST', '')).host || '',
42
+ scope: ShopifyApp.configuration.scope,
43
+ is_private: !ENV.fetch('SHOPIFY_APP_PRIVATE_SHOP', '').empty?,
44
+ is_embedded: ShopifyApp.configuration.embedded_app,
45
+ session_storage: ShopifyApp::SessionRepository,
46
+ logger: Rails.logger,
47
+ private_shop: ENV.fetch('SHOPIFY_APP_PRIVATE_SHOP', nil),
48
+ user_agent_prefix: "ShopifyApp/#{ShopifyApp::VERSION}"
49
+ )
50
+
51
+ ShopifyApp::WebhooksManager.add_registrations
52
+ end
53
+ end
@@ -0,0 +1,13 @@
1
+ var data = document.getElementById('shopify-app-init').dataset;
2
+ var AppBridge = window['app-bridge'];
3
+ var createApp = AppBridge.default;
4
+ window.app = createApp({
5
+ apiKey: data.apiKey,
6
+ host: data.host,
7
+ });
8
+
9
+ var actions = AppBridge.actions;
10
+ var TitleBar = actions.TitleBar;
11
+ TitleBar.create(app, {
12
+ title: data.page,
13
+ });
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/base"
4
+
5
+ module ShopifyApp
6
+ module Generators
7
+ class ProductsControllerGenerator < Rails::Generators::Base
8
+ source_root File.expand_path("../templates", __FILE__)
9
+
10
+ def create_products_controller
11
+ template("products_controller.rb", "app/controllers/products_controller.rb")
12
+ end
13
+
14
+ def add_products_route
15
+ route("get '/products', :to => 'products#index'")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ProductsController < AuthenticatedController
4
+ def index
5
+ @products = ShopifyAPI::Product.all(limit: 10)
6
+ render(json: { products: @products })
7
+ end
8
+ end
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails/generators/base'
3
+ require "rails/generators/base"
4
4
 
5
5
  module ShopifyApp
6
6
  module Generators
7
7
  class RotateShopifyTokenJobGenerator < Rails::Generators::Base
8
- source_root File.expand_path('../templates', __FILE__)
8
+ source_root File.expand_path("../templates", __FILE__)
9
9
 
10
10
  def add_rotate_shopify_token_job
11
- copy_file('rotate_shopify_token_job.rb', "app/jobs/shopify/rotate_shopify_token_job.rb")
12
- copy_file('rotate_shopify_token.rake', "lib/tasks/shopify/rotate_shopify_token.rake")
11
+ copy_file("rotate_shopify_token_job.rb", "app/jobs/shopify/rotate_shopify_token_job.rb")
12
+ copy_file("rotate_shopify_token.rake", "lib/tasks/shopify/rotate_shopify_token.rake")
13
13
  end
14
14
  end
15
15
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  namespace :shopify do
3
4
  desc "Rotate shopify tokens for all active shops"
4
5
  task :rotate_shopify_tokens, [:refresh_token] => :environment do |_t, args|
@@ -18,7 +18,7 @@ module Shopify
18
18
  @response = Net::HTTP.post_form(uri, post_data)
19
19
  return log_error(response_exception_error_message) unless @response.is_a?(Net::HTTPSuccess)
20
20
 
21
- access_token = JSON.parse(@response.body)['access_token']
21
+ access_token = JSON.parse(@response.body)["access_token"]
22
22
  return log_error(no_access_token_error_message) unless access_token
23
23
 
24
24
  @shop.update(shopify_token: access_token)
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
- require 'rails/generators/base'
2
+
3
+ require "rails/generators/base"
3
4
 
4
5
  module ShopifyApp
5
6
  module Generators
6
7
  class RoutesGenerator < Rails::Generators::Base
7
- source_root File.expand_path('../templates', __FILE__)
8
+ source_root File.expand_path("../templates", __FILE__)
8
9
 
9
10
  def inject_shopify_app_routes_into_application_routes
10
11
  route(session_routes)
@@ -12,9 +13,9 @@ module ShopifyApp
12
13
 
13
14
  def disable_engine_routes
14
15
  gsub_file(
15
- 'config/routes.rb',
16
+ "config/routes.rb",
16
17
  "mount ShopifyApp::Engine, at: '/'",
17
- ''
18
+ ""
18
19
  )
19
20
  end
20
21
 
@@ -25,7 +26,7 @@ module ShopifyApp
25
26
  end
26
27
 
27
28
  def routes_file_path
28
- File.expand_path(find_in_source_paths('routes.rb'))
29
+ File.expand_path(find_in_source_paths("routes.rb"))
29
30
  end
30
31
  end
31
32
  end
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  controller :sessions do
4
- get 'login' => :new, :as => :login
5
- post 'login' => :create, :as => :authenticate
6
- get 'auth/shopify/callback' => :callback
7
- get 'logout' => :destroy, :as => :logout
4
+ get "login" => :new, :as => :login
5
+ post "login" => :create, :as => :authenticate
6
+ get "auth/shopify/callback" => :callback
7
+ get "logout" => :destroy, :as => :logout
8
8
  end
9
9
 
10
10
  namespace :webhooks do
11
- post ':type' => :receive
11
+ post ":type" => :receive
12
12
  end
@@ -1,31 +1,59 @@
1
1
  # frozen_string_literal: true
2
- require 'rails/generators/base'
3
- require 'rails/generators/active_record'
2
+
3
+ require "rails/generators/base"
4
+ require "rails/generators/active_record"
4
5
 
5
6
  module ShopifyApp
6
7
  module Generators
7
8
  class ShopModelGenerator < Rails::Generators::Base
8
9
  include Rails::Generators::Migration
9
- source_root File.expand_path('../templates', __FILE__)
10
+ source_root File.expand_path("../templates", __FILE__)
11
+
12
+ class_option :new_shopify_cli_app, type: :boolean, default: false
10
13
 
11
14
  def create_shop_model
12
- copy_file('shop.rb', 'app/models/shop.rb')
15
+ copy_file("shop.rb", "app/models/shop.rb")
13
16
  end
14
17
 
15
18
  def create_shop_migration
16
- migration_template('db/migrate/create_shops.erb', 'db/migrate/create_shops.rb')
19
+ migration_template("db/migrate/create_shops.erb", "db/migrate/create_shops.rb")
20
+ end
21
+
22
+ def create_shop_with_access_scopes_migration
23
+ scopes_column_prompt = <<~PROMPT
24
+ It is highly recommended that apps record the access scopes granted by \
25
+ merchants during app installation. See app/models/shop.rb to modify how \
26
+ access scopes are stored and retrieved.
27
+
28
+ [WARNING] You will need to update the access_scopes accessors in the Shop model \
29
+ to allow shopify_app to store and retrieve scopes when going through OAuth.
30
+
31
+ The following migration will add an `access_scopes` column to the Shop model. \
32
+ Do you want to include this migration? [y/n]
33
+ PROMPT
34
+
35
+ if new_shopify_cli_app? || Rails.env.test? || yes?(scopes_column_prompt)
36
+ migration_template(
37
+ "db/migrate/add_shop_access_scopes_column.erb",
38
+ "db/migrate/add_shop_access_scopes_column.rb"
39
+ )
40
+ end
17
41
  end
18
42
 
19
43
  def update_shopify_app_initializer
20
- gsub_file('config/initializers/shopify_app.rb', 'ShopifyApp::InMemoryShopSessionStore', 'Shop')
44
+ gsub_file("config/initializers/shopify_app.rb", "ShopifyApp::InMemoryShopSessionStore", "Shop")
21
45
  end
22
46
 
23
47
  def create_shop_fixtures
24
- copy_file('shops.yml', 'test/fixtures/shops.yml')
48
+ copy_file("shops.yml", "test/fixtures/shops.yml")
25
49
  end
26
50
 
27
51
  private
28
52
 
53
+ def new_shopify_cli_app?
54
+ options["new_shopify_cli_app"]
55
+ end
56
+
29
57
  def rails_migration_version
30
58
  Rails.version.match(/\d\.\d/)[0]
31
59
  end
@@ -0,0 +1,5 @@
1
+ class AddShopAccessScopesColumn < ActiveRecord::Migration[<%= rails_migration_version %>]
2
+ def change
3
+ add_column :shops, :access_scopes, :string
4
+ end
5
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  class Shop < ActiveRecord::Base
3
- include ShopifyApp::ShopSessionStorage
4
+ include ShopifyApp::ShopSessionStorageWithScopes
4
5
 
5
6
  def api_version
6
7
  ShopifyApp.configuration.api_version
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ShopifyApp
3
4
  module Generators
4
5
  class ShopifyAppGenerator < Rails::Generators::Base
@@ -8,10 +9,10 @@ module ShopifyApp
8
9
  end
9
10
 
10
11
  def run_all_generators
11
- generate("shopify_app:install #{@opts.join(' ')}")
12
- generate("shopify_app:shop_model")
12
+ generate("shopify_app:install #{@opts.join(" ")}")
13
+ generate("shopify_app:shop_model #{@opts.join(" ")}")
13
14
  generate("shopify_app:authenticated_controller")
14
- generate("shopify_app:home_controller")
15
+ generate("shopify_app:home_controller #{@opts.join(" ")}")
15
16
  end
16
17
  end
17
18
  end
@@ -0,0 +1,5 @@
1
+ class AddUserAccessScopesColumn < ActiveRecord::Migration[<%= rails_migration_version %>]
2
+ def change
3
+ add_column :users, :access_scopes, :string
4
+ end
5
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  class User < ActiveRecord::Base
3
- include ShopifyApp::UserSessionStorage
4
+ include ShopifyApp::UserSessionStorageWithScopes
4
5
 
5
6
  def api_version
6
7
  ShopifyApp.configuration.api_version