shopify_app 14.4.4 → 18.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) 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 +17 -1
  7. data/.github/workflows/build.yml +41 -0
  8. data/.github/workflows/release.yml +24 -0
  9. data/.github/workflows/rubocop.yml +1 -7
  10. data/.gitignore +0 -2
  11. data/CHANGELOG.md +75 -0
  12. data/CONTRIBUTING.md +76 -0
  13. data/Gemfile.lock +268 -0
  14. data/README.md +73 -572
  15. data/app/assets/images/storage_access.svg +1 -2
  16. data/app/assets/javascripts/shopify_app/post_redirect.js +9 -0
  17. data/app/controllers/concerns/shopify_app/ensure_authenticated_links.rb +26 -0
  18. data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +32 -0
  19. data/app/controllers/shopify_app/callback_controller.rb +34 -10
  20. data/app/controllers/shopify_app/sessions_controller.rb +19 -20
  21. data/app/views/shopify_app/partials/_button_styles.html.erb +41 -36
  22. data/app/views/shopify_app/partials/_card_styles.html.erb +3 -3
  23. data/app/views/shopify_app/partials/_empty_state_styles.html.erb +28 -59
  24. data/app/views/shopify_app/partials/_form_styles.html.erb +56 -0
  25. data/app/views/shopify_app/partials/_layout_styles.html.erb +16 -1
  26. data/app/views/shopify_app/partials/_typography_styles.html.erb +6 -6
  27. data/app/views/shopify_app/sessions/enable_cookies.html.erb +1 -1
  28. data/app/views/shopify_app/sessions/new.html.erb +38 -110
  29. data/app/views/shopify_app/sessions/request_storage_access.html.erb +1 -1
  30. data/app/views/shopify_app/sessions/top_level_interaction.html.erb +20 -15
  31. data/app/views/shopify_app/shared/post_redirect_to_auth_shopify.html.erb +13 -0
  32. data/config/locales/de.yml +11 -11
  33. data/config/locales/nl.yml +1 -1
  34. data/config/locales/vi.yml +22 -0
  35. data/config/locales/zh-CN.yml +1 -1
  36. data/docs/Quickstart.md +15 -77
  37. data/docs/Releasing.md +9 -6
  38. data/docs/Troubleshooting.md +147 -4
  39. data/docs/Upgrading.md +126 -0
  40. data/docs/shopify_app/authentication.md +124 -0
  41. data/docs/shopify_app/engine.md +82 -0
  42. data/docs/shopify_app/generators.md +127 -0
  43. data/docs/shopify_app/handling-access-scopes-changes.md +14 -0
  44. data/docs/shopify_app/script-tags.md +28 -0
  45. data/docs/shopify_app/session-repository.md +88 -0
  46. data/docs/shopify_app/testing.md +38 -0
  47. data/docs/shopify_app/webhooks.md +72 -0
  48. data/lib/generators/shopify_app/home_controller/home_controller_generator.rb +16 -7
  49. data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +10 -0
  50. data/lib/generators/shopify_app/home_controller/templates/index.html.erb +11 -11
  51. data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +2 -0
  52. data/lib/generators/shopify_app/install/install_generator.rb +36 -2
  53. data/lib/generators/shopify_app/install/templates/embedded_app.html.erb +2 -1
  54. data/lib/generators/shopify_app/install/templates/omniauth.rb +1 -0
  55. data/lib/generators/shopify_app/install/templates/shopify_app.js +1 -1
  56. data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +13 -4
  57. data/lib/generators/shopify_app/install/templates/shopify_provider.rb.tt +8 -0
  58. data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +27 -0
  59. data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
  60. data/lib/generators/shopify_app/shop_model/templates/shop.rb +1 -1
  61. data/lib/generators/shopify_app/shopify_app_generator.rb +1 -1
  62. data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
  63. data/lib/generators/shopify_app/user_model/templates/user.rb +1 -1
  64. data/lib/generators/shopify_app/user_model/user_model_generator.rb +27 -0
  65. data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
  66. data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
  67. data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
  68. data/lib/shopify_app/configuration.rb +25 -0
  69. data/lib/shopify_app/controller_concerns/itp.rb +0 -2
  70. data/lib/shopify_app/controller_concerns/login_protection.rb +14 -17
  71. data/lib/shopify_app/engine.rb +1 -0
  72. data/lib/shopify_app/middleware/same_site_cookie_middleware.rb +1 -1
  73. data/lib/shopify_app/omniauth/omniauth_configuration.rb +64 -0
  74. data/lib/shopify_app/session/in_memory_shop_session_store.rb +9 -7
  75. data/lib/shopify_app/session/in_memory_user_session_store.rb +9 -7
  76. data/lib/shopify_app/session/jwt.rb +3 -1
  77. data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +58 -0
  78. data/lib/shopify_app/session/user_session_storage_with_scopes.rb +58 -0
  79. data/lib/shopify_app/utils.rb +12 -0
  80. data/lib/shopify_app/version.rb +1 -1
  81. data/lib/shopify_app.rb +11 -0
  82. data/package.json +1 -1
  83. data/service.yml +1 -4
  84. data/shopify_app.gemspec +5 -4
  85. data/translation.yml +1 -1
  86. data/yarn.lock +92 -123
  87. metadata +62 -16
  88. data/.github/ISSUE_TEMPLATE.md +0 -19
  89. data/.travis.yml +0 -27
  90. data/docs/install-on-dev-shop.png +0 -0
  91. data/docs/test-your-app.png +0 -0
  92. data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -20
data/README.md CHANGED
@@ -1,630 +1,131 @@
1
- Shopify App
2
- ===========
3
- [![Version][gem]][gem_url] [![Build Status](https://travis-ci.org/Shopify/shopify_app.png)](https://travis-ci.org/Shopify/shopify_app)
1
+ # Shopify App
2
+
3
+ **Shopify is doubling our engineering staff in 2021! [Join our team and work on libraries like this one.](https://smrtr.io/5GGrK)**
4
+
5
+ [![Version][gem]][gem_url] [![Build Status](https://github.com/Shopify/shopify_app/workflows/CI/badge.svg)](https://github.com/Shopify/shopify_app/actions?query=workflow%3ACI) ![Supported Rails version][supported_rails_version]
4
6
 
5
7
  [gem]: https://img.shields.io/gem/v/shopify_app.svg
6
8
  [gem_url]: https://rubygems.org/gems/shopify_app
9
+ [supported_rails_version]: https://img.shields.io/badge/rails-%3C6.2.0-orange
7
10
 
11
+ This gem builds Rails applications that can be embedded in the Shopify Admin.
8
12
 
9
- Shopify Application Rails engine and generator
10
-
11
- #### NOTE: Versions 8.0.0 through 8.2.3 contained a CSRF vulnerability that was addressed in version 8.2.4. Please update to version 8.2.4 if you're using an old version.
13
+ [Introduction](#introduction) |
14
+ [Requirements](#requirements) |
15
+ [Usage](#usage) |
16
+ [Documentation](#documentation) |
17
+ [Contributing](/CONTRIBUTING.md) |
18
+ [License](/LICENSE)
12
19
 
13
- Table of Contents
14
- -----------------
15
- - [Introduction](#introduction)
16
- - [Become a Shopify App Developer](#become-a-shopify-app-developer)
17
- - [Installation](#installation)
18
- - [Generators](#generators)
19
- - [Mounting the Engine](#mounting-the-engine)
20
- - [Authentication](#authentication)
21
- - [WebhooksManager](#webhooksmanager)
22
- - [ScripttagsManager](#scripttagsmanager)
23
- - [RotateShopifyTokenJob](#rotateshopifytokenjob)
24
- - [App Tunneling](#app-tunneling)
25
- - [AppProxyVerification](#appproxyverification)
26
- - [Troubleshooting](#troubleshooting)
27
- - [Testing an embedded app outside the Shopify admin](#testing-an-embedded-app-outside-the-shopify-admin)
28
- - [Migration to 13.0.0](#migrating-to-1300)
29
- - [Questions or problems?](#questions-or-problems)
30
- - [Rails 6 Compatibility](#rails-6-compatibility)
31
- - [Upgrading from 8.6 to 9.0.0](#upgrading-from-86-to-900)
20
+ ## Introduction
32
21
 
33
- Introduction
34
- -----------
35
- Get started with the [Shopify Admin API](https://help.shopify.com/en/api/getting-started) faster; This gem includes a Rails Engine and generators for writing Rails applications using the Shopify API. The Engine provides a SessionsController and all the required code for authenticating with a shop via OAuth (other authentication methods are not supported).
22
+ This gem includes a Rails engine, generators, modules, and mixins that help create Rails applications that work with Shopify APIs. The [Shopify App Rails engine](/docs/shopify_app/engine.md) provides all the code required to implement OAuth with Shopify. The [default Shopify App generator](/docs/shopify_app/generators.md#-environment-rails-generate-shopify_app) builds an app that can be embedded in the Shopify Admin and secures it with [session tokens](https://shopify.dev/concepts/apps/building-embedded-apps-using-session-tokens).
36
23
 
37
- *Note: It's recommended to use this on a new Rails project so that the generator won't overwrite/delete your files.*
24
+ <!-- This section is linked to in `templates/shopify_app.rb.tt`. Be careful renaming this heading. -->
25
+ ## Requirements
38
26
 
39
- Learn how to create and deploy a new Shopify App to Heroku with our [quickstart guide](https://github.com/Shopify/shopify_app/blob/master/docs/Quickstart.md), or dive in in less than 5 minutes with this quickstart video:
27
+ > **Rails compatibility**
28
+ > * Use Shopify App `<= v7.2.8` if you need to work with Rails 4.
40
29
 
41
- [https://www.youtube.com/watch?v=yGxeoAHlQOg](https://www.youtube.com/watch?v=yGxeoAHlQOg)
30
+ To become a Shopify app developer, you will need a [Shopify Partners](https://www.shopify.com/partners) account. Explore the [Shopify dev docs](https://shopify.dev/concepts/shopify-introduction) to learn more about [building Shopify apps](https://shopify.dev/concepts/apps).
42
31
 
43
- Become a Shopify App Developer
44
- --------------------------------
45
- To become a Shopify App Developer, you'll need a [Shopify Partner account.](http://shopify.com/partners) If you don't have a Shopify Partner account, head to http://shopify.com/partners to create one before you start.
32
+ This gem requires that you have the following credentials:
46
33
 
47
- Once you have a Partner account, [create a new application in the Partner Dashboard](https://help.shopify.com/en/api/tools/partner-dashboard/your-apps) to get an API key and other API credentials.
34
+ - **Shopify API key:** The API key app credential specified in your [Shopify Partners dashboard](https://partners.shopify.com/organizations).
35
+ - **Shopify API secret:** The API secret key app credential specified in your [Shopify Partners dashboard](https://partners.shopify.com/organizations).
48
36
 
49
- To create an application for development set your new app's `App URL` to the URL provided by [your tunnel](#app-tunneling), ensuring that you use `https://`. If you are not planning to embed your app inside the Shopify admin or receive webhooks, set your redirect URL to `http://localhost:3000/` and the `Whitelisted redirection URL(s)` to contain `<App URL>/auth/shopify/callback`.
37
+ ## Usage
50
38
 
51
- Installation
52
- ------------
53
- To get started, add `shopify_app` to your Gemfile and run `bundle install`:
39
+ 1. To get started, create a new Rails app:
54
40
 
55
41
  ``` sh
56
- # Create a new rails app
57
42
  $ rails new my_shopify_app
58
- $ cd my_shopify_app
59
-
60
- # Add the gem shopify_app to your Gemfile
61
- $ bundle add shopify_app
62
- ```
63
-
64
- Now we are ready to run any of the [generators](#generators) included with `shopify_app`. The following section explains the generators and what you can do with them.
65
-
66
-
67
- #### Rails Compatibility
68
-
69
- The latest version of shopify_app is compatible with Rails `>= 5`. Use version `<= v7.2.8` if you need to work with Rails 4.
70
-
71
-
72
- Generators
73
- ----------
74
-
75
- ### Default Generator
76
-
77
- The default generator will run the `install`, `shop`, `authenticated_controller`, and `home_controller` generators. This is the recommended way to start a new app from scratch:
78
-
79
- ```sh
80
- $ rails generate shopify_app
81
- ```
82
-
83
- After running the generator, you will need to run `rails db:migrate` to add new tables to your database. You can start your app with `bundle exec rails server` and install your app by visiting `http://localhost` in your web browser.
84
-
85
- ### API Keys
86
-
87
- The default and install generators have been updated to source Shopify API key and secret from an Environment (`.env`) variables file, which you will need to create with the following format:
88
-
89
- ```
90
- SHOPIFY_API_KEY=your api key
91
- SHOPIFY_API_SECRET=your api secret
92
- ```
93
-
94
- These values can be found on the "App Setup" page in the [Shopify Partners Dashboard][dashboard]. If you are checking your code into a code repository, ensure your `.gitignore` prevents your `.env` file from being checked into any publicly accessible code.
95
-
96
- **You will need to load the ENV variables into your environment, you can do this with the [dot-env](https://github.com/bkeepers/dotenv) gem or any other method you wish to.**
97
-
98
- ### Install Generator
99
-
100
- ```sh
101
- $ rails generate shopify_app:install
102
- ```
103
-
104
- Options include:
105
- * `application_name` - the name of your app, it can be supplied with or without double-quotes if a whitespace is present. (e.g. `--application_name Example App` or `--application_name "Example App"`)
106
- * `scope` - the OAuth access scope required for your app, e.g. **read_products, write_orders**. *Multiple options* need to be delimited by a comma-space and can be supplied with or without double-quotes
107
- (e.g. `--scope read_products, write_orders, write_products` or `--scope "read_products, write_orders, write_products"`)
108
- For more information, refer to the [docs](http://docs.shopify.com/api/tutorials/oauth).
109
- * `embedded` - the default is to generate an [embedded app](http://docs.shopify.com/embedded-app-sdk), if you want a legacy non-embedded app then set this to false, `--embedded false`
110
-
111
- You can update any of these settings later on easily; the arguments are simply for convenience.
112
-
113
- The generator adds ShopifyApp and the required initializers to the host Rails application.
114
-
115
- After running the `install` generator, you can start your app with `bundle exec rails server` and install your app by visiting localhost.
116
-
117
-
118
- ### Home Controller Generator
119
-
120
- ```sh
121
- $ rails generate shopify_app:home_controller
122
- ```
123
-
124
- This generator creates an example home controller and view which fetches and displays products using the Shopify API.
125
-
126
- Options include:
127
- * __[beta]__ `with-session-token`: This flag generates an unauthenticated home_controller and a protected sample products_controller. It also creates a home view that leverages a session token to fetch products from your products_controller. Use this flag if you plan to build a single-page application or to secure your app using JWT session tokens (e.g. `--with-session-token` or `--with-session-token true`).
128
-
129
- ### Products Controller Generator
130
-
131
- ```sh
132
- $ rails generate shopify_app:products_controller
133
43
  ```
134
44
 
135
- This generator creates an example products API controller to fetch products using the Shopify API.
136
-
137
- ### App Proxy Controller Generator
45
+ 2. Add the Shopify App gem to `my_shopify_app`'s Gemfile.
138
46
 
139
47
  ```sh
140
- $ rails generate shopify_app:app_proxy_controller
141
- ```
142
-
143
- This optional generator, not included with the default generator, creates the app proxy controller to handle proxy requests to the app from your shop storefront, modifies 'config/routes.rb' with a namespace route, and an example view which displays current shop information using the LiquidAPI.
144
-
145
- ### Marketing Extension Generator
146
-
147
- ```sh
148
- $ rails generate shopify_app:add_marketing_activity_extension
149
- ```
150
-
151
- This will create a controller with the endpoints required to build a [marketing activities extension](https://help.shopify.com/en/api/embedded-apps/app-extensions/shopify-admin/marketing-activities). The extension will be generated with a base URL at `/marketing_activities`, which should also be configured in partners.
152
-
153
- ### Controllers, Routes and Views
154
-
155
- The last group of generators are for your convenience if you want to start overriding code included as part of the Rails engine. For example, by default the engine provides a simple SessionController, if you run the `rails generate shopify_app:controllers` generator then this code gets copied out into your app so you can start adding to it. Routes and views follow the exact same pattern.
156
-
157
- Mounting the Engine
158
- -------------------
159
-
160
- Mounting the Engine will provide the basic routes to authenticating a shop with your application. By default it will provide:
161
-
162
- | Verb | Route | Action |
163
- |--------|-------------------------------|------------------------------|
164
- |GET |'/login' |Login |
165
- |POST |'/login' |Login |
166
- |GET |'/auth/shopify/callback' |Authenticate Callback |
167
- |GET |'/logout' |Logout |
168
- |POST |'/webhooks/:type' |Webhook Callback |
169
-
170
- ### Nested Routes
171
-
172
- The engine may also be mounted at a nested route, for example:
173
-
174
- ```ruby
175
- mount ShopifyApp::Engine, at: '/nested'
176
- ```
177
-
178
- This will create the Shopify engine routes under the specified subpath. You'll also need to make some updates to your `shopify_app.rb` and `omniauth.rb` initializers. First, update the shopify_app initializer to include a custom `root_url` e.g.:
179
-
180
- ```ruby
181
- ShopifyApp.configure do |config|
182
- config.root_url = '/nested'
183
- end
184
- ```
185
-
186
- then update the omniauth initializer to include a custom `callback_path` e.g.:
187
-
188
- ```ruby
189
- provider :shopify,
190
- ShopifyApp.configuration.api_key,
191
- ShopifyApp.configuration.secret,
192
- scope: ShopifyApp.configuration.scope,
193
- callback_path: '/nested/auth/shopify/callback'
48
+ $ bundle add shopify_app
194
49
  ```
195
50
 
196
- You may also need to change your `config/routes.rb` to render a view for `/nested`, since this is what will be rendered in the Shopify Admin of any shops that have installed your app. The engine itself doesn't have a view for this, so you'll need something like this:
51
+ 3. Create a `.env` file in the root of `my_shopify_app` to specify your Shopify API credentials:
197
52
 
198
- ```ruby
199
- # config/routes.rb
200
- Rails.application.routes.draw do
201
- root :to => 'something_else#index'
202
- get "/nested", to: "home#index"
203
- mount ShopifyApp::Engine, at: '/nested'
204
- end
205
53
  ```
206
-
207
- Finally, note that if you do this, to add your app to a store, you must navigate to `/nested` in order to render the `Enter your shop domain to log in or install this app.` UI.
208
-
209
- ### Custom login URL
210
-
211
- While you can customize the login view by creating a `/app/views/shopify_app/sessions/new.html.erb` file, you may also want to customize the URL entirely. You can modify your `shopify_app.rb` initializer to provide a custom `login_url` e.g.:
212
-
213
- ```ruby
214
- ShopifyApp.configure do |config|
215
- config.login_url = 'https://my.domain.com/nested/login'
216
- end
54
+ SHOPIFY_API_KEY=<Your Shopify API key>
55
+ SHOPIFY_API_SECRET=<Your Shopify API secret>
217
56
  ```
218
57
 
219
- Authentication
220
- --------------
221
-
222
- ### Callback
58
+ > In a development environment, you can use a gem like `dotenv-rails` to manage environment variables.
223
59
 
224
- Upon completing the authentication flow, Shopify calls the app at the `callback_path` mentioned before. If the app needs to do some extra work, it can define and configure the route to a custom callback controller, inheriting from `ShopifyApp::CallbackController` and hook into or override any of the defined helper methods. The default callback controller already provides the following behaviour:
225
- * Logging into the shop and resetting the session
226
- * [Installing Webhooks](https://github.com/Shopify/shopify_app#webhooksmanager)
227
- * [Setting Scripttags](https://github.com/Shopify/shopify_app#scripttagsmanager)
228
- * [Performing the AfterAuthenticate Job](https://github.com/Shopify/shopify_app#afterauthenticatejob)
229
- * Redirecting to the return address
60
+ 4. Run the default Shopify App generator to create an app that can be embedded in the Shopify Admin:
230
61
 
231
- **Note that starting with version 8.4.0, we have extracted the callback logic in its own controller. If you are upgrading from a version older than 8.4.0 the callback action and related helper methods were defined in `ShopifyApp::SessionsController` ==> you will have to extend `ShopifyApp::CallbackController` instead and port your logic to the new controller.**
232
-
233
- ### ShopifyApp::SessionRepository
234
-
235
- `ShopifyApp::SessionRepository` allows you as a developer to define how your sessions are stored and retrieved for shops. The `SessionRepository` is configured in the `config/initializers/shopify_app.rb` file and can be set to any object that implements `self.store(auth_session, *args)` which stores the session and returns a unique identifier and `self.retrieve(id)` which returns a `ShopifyAPI::Session` for the passed id. These methods are already implemented as part of the `ShopifyApp::SessionStorage` concern but can be overridden for custom implementation.
236
-
237
- #### Shop-based token storage
238
- Storing tokens on the store model means that any user login associated with the store will have equal access levels to whatever the original user granted the app.
239
- ```sh
240
- $ rails generate shopify_app:shop_model
241
- ```
242
- This will generate a shop model which will be the storage for the tokens necessary for authentication.
243
-
244
- #### User-based token storage
245
- A more granular control over the level of access per user on an app might be necessary, to which the shop-based token strategy is not sufficient. Shopify supports a user-based token storage strategy where a unique token to each user can be managed. Shop tokens must still be maintained if you are running background jobs so that you can make use of them when necessary.
246
62
  ```sh
247
- $ rails generate shopify_app:shop_model
248
- $ rails generate shopify_app:user_model
249
- ```
250
- This will generate a shop model and user model, which will be the storage for the tokens necessary for authentication.
251
-
252
- The current Shopify user will be stored in the rails session at `session[:shopify_user]`
253
-
254
- Read more about Online vs. Offline access [here](https://help.shopify.com/api/getting-started/authentication/oauth).
255
-
256
- #### Migrating from shop-based to user-based token strategy
257
- 1. Run the `user_model` generator as mentioned above.
258
- 2. Ensure that both your `Shop` model and `User` model includes the necessary concerns `ShopifyApp::ShopSessionStorage` and `ShopifyApp::UserSessionStorage`.
259
- 3. Make changes to 2 initializer files as shown below:
260
- ```ruby
261
- # In the `omniauth.rb` initializer:
262
- provider :shopify,
263
- ...
264
- setup: lambda { |env|
265
- ...
266
- # Add this line
267
- strategy.options[:per_user_permissions] = strategy.session[:user_tokens]
268
- ...
269
- }
270
-
271
- # In the `shopify_app.rb` initializer:
272
- config.shop_session_repository = {YOUR_SHOP_MODEL_CLASS}
273
- config.user_session_repository = {YOUR_USER_MODEL_CLASS}
274
- ```
275
-
276
- ### Authenticated
277
-
278
- The engine provides a `ShopifyApp::Authenticated` concern which should be included in any controller that is intended to be behind Shopify OAuth. It adds `before_action`s to ensure that the user is authenticated and will redirect to the Shopify login page if not. It is best practice to include this concern in a base controller inheriting from your `ApplicationController`, from which all controllers that require Shopify authentication inherit.
279
-
280
- For backwards compatibility, the engine still provides a controller called `ShopifyApp::AuthenticatedController` which includes the `ShopifyApp::Authenticated` concern. Note that it inherits directly from `ActionController::Base`, so you will not be able to share functionality between it and your application's `ApplicationController`.
281
-
282
- ### AfterAuthenticate Job
283
-
284
- If your app needs to perform specific actions after the user is authenticated successfully (i.e. every time a new session is created), ShopifyApp can queue or run a job of your choosing (note that we already provide support for automatically creating Webhooks and Scripttags). To configure the after authenticate job, update your initializer as follows:
285
-
286
- ```ruby
287
- ShopifyApp.configure do |config|
288
- config.after_authenticate_job = { job: "Shopify::AfterAuthenticateJob" }
289
- end
290
- ```
291
-
292
- The job can be configured as either a class or a class name string.
293
-
294
- If you need the job to run synchronously add the `inline` flag:
295
-
296
- ```ruby
297
- ShopifyApp.configure do |config|
298
- config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: true }
299
- end
300
- ```
301
-
302
- We've also provided a generator which creates a skeleton job and updates the initializer for you:
303
-
304
- ```
305
- bin/rails g shopify_app:add_after_authenticate_job
306
- ```
307
-
308
- If you want to perform that action only once, e.g. send a welcome email to the user when they install the app, you should make sure that this action is idempotent, meaning that it won't have an impact if run multiple times.
309
-
310
- API Versioning
311
- --------------
312
-
313
- Shopify's API is versioned, and you can [read about that process in the Shopify Developers documentation page](https://shopify.dev/concepts/about-apis/versioning).
314
-
315
- Since shopify_app gem version 1.11.0, the included shopify_api gem has also been updated to allow you to easily set and switch what version of the Shopify API you want your app or service to use, as well as surface warnings to Rails apps about [deprecated endpoints, GraphQL fields and more](https://shopify.dev/concepts/about-apis/versioning#deprecation-practices).
316
-
317
- See the [shopify_api gem README](https://github.com/Shopify/shopify_api/) for more details.
318
-
319
- WebhooksManager
320
- ---------------
321
-
322
- ShopifyApp can manage your app's webhooks for you if you set which webhooks you require in the initializer:
323
-
324
- ```ruby
325
- ShopifyApp.configure do |config|
326
- config.webhooks = [
327
- {topic: 'carts/update', address: 'https://example-app.com/webhooks/carts_update'}
328
- ]
329
- end
330
- ```
331
-
332
- When the OAuth callback is completed successfully, ShopifyApp will queue a background job which will ensure all the specified webhooks exist for that shop. Because this runs on every OAuth callback, it means your app will always have the webhooks it needs even if the user uninstalls and re-installs the app.
333
-
334
- ShopifyApp also provides a WebhooksController that receives webhooks and queues a job based on the received topic. For example, if you register the webhook from above, then all you need to do is create a job called `CartsUpdateJob`. The job will be queued with 2 params: `shop_domain` and `webhook` (which is the webhook body).
335
-
336
- If you would like to namespace your jobs, you may set `webhook_jobs_namespace` in the config. For example, if your app handles webhooks from other ecommerce applications as well, and you want Shopify cart update webhooks to be processed by a job living in `jobs/shopify/webhooks/carts_update_job.rb` rather than `jobs/carts_update_job.rb`):
337
-
338
- ```ruby
339
- ShopifyApp.configure do |config|
340
- config.webhook_jobs_namespace = 'shopify/webhooks'
341
- end
342
- ```
343
-
344
- If you are only interested in particular fields, you can optionally filter the data sent by Shopify by specifying the `fields` parameter in `config/webhooks`. Note that you will still receive a webhook request from Shopify every time the resource is updated, but only the specified fields will be sent.
345
-
346
- ```ruby
347
- ShopifyApp.configure do |config|
348
- config.webhooks = [
349
- {topic: 'products/update', address: 'https://example-app.com/webhooks/products_update', fields: ['title', 'vendor']}
350
- ]
351
- end
352
- ```
353
-
354
- If you'd rather implement your own controller then you'll want to use the WebhookVerification module to verify your webhooks, example:
355
-
356
- ```ruby
357
- class CustomWebhooksController < ApplicationController
358
- include ShopifyApp::WebhookVerification
359
-
360
- def carts_update
361
- params.permit!
362
- SomeJob.perform_later(shop_domain: shop_domain, webhook: webhook_params.to_h)
363
- head :no_content
364
- end
365
-
366
- private
367
-
368
- def webhook_params
369
- params.except(:controller, :action, :type)
370
- end
371
- end
372
- ```
373
-
374
- The module skips the `verify_authenticity_token` before_action and adds an action to verify that the webhook came from Shopify. You can now add a post route to your application, pointing to the controller and action to accept the webhook data from Shopify.
375
-
376
- The WebhooksManager uses ActiveJob. If ActiveJob is not configured then by default Rails will run the jobs inline. However, it is highly recommended to configure a proper background processing queue like Sidekiq or Resque in production.
377
-
378
- ShopifyApp can create webhooks for you using the `add_webhook` generator. This will add the new webhook to your config and create the required job class for you.
379
-
380
- ```
381
- rails g shopify_app:add_webhook -t carts/update -a https://example.com/webhooks/carts_update
382
- ```
383
-
384
- Where `-t` is the topic and `-a` is the address the webhook should be sent to.
385
-
386
- ScripttagsManager
387
- -----------------
388
-
389
- As with webhooks, ShopifyApp can manage your app's scripttags for you by setting which scripttags you require in the initializer:
390
-
391
- ```ruby
392
- ShopifyApp.configure do |config|
393
- config.scripttags = [
394
- {event:'onload', src: 'https://my-shopifyapp.herokuapp.com/fancy.js'},
395
- {event:'onload', src: ->(domain) { dynamic_tag_url(domain) } }
396
- ]
397
- end
398
- ```
399
-
400
- You also need to have write_script_tags permission in the config scope in order to add script tags automatically:
401
-
402
- ```ruby
403
- config.scope = '... , write_script_tags'
404
- ```
405
-
406
- Scripttags are created in the same way as the Webhooks, with a background job which will create the required scripttags.
407
-
408
- If `src` responds to `call` its return value will be used as the scripttag's source. It will be called on scripttag creation and deletion.
409
-
410
- RotateShopifyTokenJob
411
- ---------------------
412
-
413
- If your Shopify secret key is leaked, you can use the RotateShopifyTokenJob to perform [API Credential Rotation](https://help.shopify.com/en/api/getting-started/authentication/oauth/api-credential-rotation).
414
-
415
- Before running the job, you'll need to generate a new secret key from your Shopify Partner dashboard, and update the `/config/initializers/shopify_app.rb` to hold your new and old secret keys:
416
-
417
- ```ruby
418
- config.secret = Rails.application.secrets.shopify_secret
419
- config.old_secret = Rails.application.secrets.old_shopify_secret
63
+ $ rails generate shopify_app
420
64
  ```
421
65
 
422
- We've provided a generator which creates the job and an example rake task:
66
+ 5. Run a migration to create the necessary tables in your database:
423
67
 
424
68
  ```sh
425
- bin/rails g shopify_app:rotate_shopify_token_job
426
- ```
427
-
428
- The generated rake task will be found at `lib/tasks/shopify/rotate_shopify_token.rake` and is provided strictly for example purposes. It might not work with your application out of the box without some configuration.
429
-
430
- ⚠️ Note: if you are updating `shopify_app` from a version prior to 8.4.2 (and do not wish to run the default/install generator again), you will need to add [the following line](https://github.com/Shopify/shopify_app/blob/4f7e6cca2a472d8f7af44b938bd0fcafe4d8e88a/lib/generators/shopify_app/install/templates/shopify_provider.rb#L18) to `config/initializers/omniauth.rb`:
431
-
432
- ```ruby
433
- strategy.options[:old_client_secret] = ShopifyApp.configuration.old_secret
69
+ $ rails db:migrate
434
70
  ```
435
71
 
436
- App Tunneling
437
- -------------
438
-
439
- Your local app needs to be accessible from the public Internet in order to install it on a Shopify store, to use the [App Proxy Controller](#app-proxy-controller-generator) or receive Webhooks.
440
-
441
- Use a tunneling service like [ngrok](https://ngrok.com/), [Forward](https://forwardhq.com/), [Beeceptor](https://beeceptor.com/), [Mockbin](http://mockbin.org/), or [Hookbin](https://hookbin.com/) to make your development environment accessible to the internet.
442
-
443
- For example with [ngrok](https://ngrok.com/), run this command to set up a tunnel proxy to Rails' default port:
72
+ 6. Run the app:
444
73
 
445
74
  ```sh
446
- ngrok http 3000
447
- ```
448
-
449
- AppProxyVerification
450
- --------------------
451
-
452
- The engine provides a mixin for verifying incoming HTTP requests sent via an App Proxy. Any controller that `include`s `ShopifyApp::AppProxyVerification` will verify that each request has a valid `signature` query parameter that is calculated using the other query parameters and the app's shared secret.
453
-
454
- ### Recommended Usage
455
-
456
- The App Proxy Controller Generator automatically adds the mixin to the generated app_proxy_controller.rb
457
- Additional controllers for resources within the App_Proxy namespace, will need to include the mixin like so:
458
-
459
- ```ruby
460
- # app/controllers/app_proxy/reviews_controller.rb
461
- class ReviewsController < ApplicationController
462
- include ShopifyApp::AppProxyVerification
463
- # ...
464
- end
465
- ```
466
-
467
- Create your app proxy URL in the [Shopify Partners' Dashboard][dashboard], making sure to point it to `https://your_app_website.com/app_proxy`.
468
- ![Creating an App Proxy](/images/app-proxy-screenshot.png)
469
-
470
- App Bridge
471
- ---
472
-
473
- A basic example of using [App Bridge][app-bridge] is included in the install generator. An app instance is automatically initialized in [shopify_app.js](https://github.com/Shopify/shopify_app/blob/master/lib/generators/shopify_app/install/templates/shopify_app.js) and [flash_messages.js](https://github.com/Shopify/shopify_app/blob/master/lib/generators/shopify_app/install/templates/flash_messages.js) converts Rails [flash messages](https://api.rubyonrails.org/classes/ActionDispatch/Flash.html) to App Bridge Toast actions automatically. By default, this library is included via [unpkg in the embedded_app layout](https://github.com/Shopify/shopify_app/blob/master/lib/generators/shopify_app/install/templates/embedded_app.html.erb#L27). For more advanced uses it is recommended to [install App Bridge via npm or yarn](https://help.shopify.com/en/api/embedded-apps/app-bridge/getting-started#set-up-shopify-app-bridge-in-your-app).
474
-
475
- Troubleshooting
476
- ---------------
477
-
478
- see [TROUBLESHOOTING.md](https://github.com/Shopify/shopify_app/blob/master/docs/Troubleshooting.md)
479
-
480
- Using Test Helpers inside your Application
481
- -----------------------------------------
482
-
483
- A test helper that will allow you to test `ShopifyApp::WebhookVerification` in the controller from your app, to use this test, you need to `require` it directly inside your app `test/controllers/webhook_verification_test.rb`.
484
-
485
- ```ruby
486
- require 'test_helper'
487
- require 'action_controller'
488
- require 'action_controller/base'
489
- require 'shopify_app/test_helpers/webhook_verification_helper'
490
- ```
491
-
492
- Or you can require in your `test/test_helper.rb`.
493
-
494
- ```ruby
495
- ENV['RAILS_ENV'] ||= 'test'
496
- require_relative '../config/environment'
497
- require 'rails/test_help'
498
- require 'byebug'
499
- require 'shopify_app/test_helpers/all'
75
+ $ rails server
500
76
  ```
501
77
 
502
- With `lib/shopify_app/test_helpers/all'` more tests can be added and will only need to be required in once in your library.
503
-
78
+ See [*Quickstart*](/docs/Quickstart.md) to learn how to install your app on a shop.
504
79
 
505
- Testing an embedded app outside the Shopify admin
506
- -------------------------------------------------
80
+ This app implements [OAuth 2.0](https://shopify.dev/tutorials/authenticate-with-oauth) with Shopify to authenticate requests made to Shopify APIs. By default, this app is configured to use [session tokens](https://shopify.dev/concepts/apps/building-embedded-apps-using-session-tokens) to authenticate merchants when embedded in the Shopify Admin.
507
81
 
508
- By default, loading your embedded app will redirect to the Shopify admin, with the app view loaded in an `iframe`. If you need to load your app outside of the Shopify admin (e.g., for performance testing), you can change `forceRedirect: true` to `false` in `ShopifyApp.init` block in the `embedded_app` view. To keep the redirect on in production but off in your `development` and `test` environments, you can use:
82
+ See [*Generators*](/docs/shopify_app/generators.md) for a complete list of generators available to Shopify App.
509
83
 
510
- ```javascript
511
- forceRedirect: <%= Rails.env.development? || Rails.env.test? ? 'false' : 'true' %>
512
- ```
84
+ ## Documentation
513
85
 
514
- Migrating to 13.0.0
515
- -------------------
86
+ You can find documentation on gem usage, concepts, mixins, installation, and more in [`/docs`](/docs).
516
87
 
517
- Version 13.0.0 adds the ability to use both user and shop sessions, concurrently. This however involved a large
518
- change to how session stores work. Here are the steps to migrate to 13.x
88
+ * Start with the [*Generators*](/docs/shopify_app/generators.md) document to learn more about the generators this gem offers.
89
+ * Check out the [*Changelog*](/CHANGELOG.md) for notes on the latest gem releases.
90
+ * See [*Troubleshooting*](/docs/Troubleshooting.md) for tips on common issues.
91
+ * If you are looking to upgrade your Shopify App version to a new major release, see [*Upgrading*](/docs/Upgrading.md) for important notes on breaking changes.
519
92
 
520
- ### Changes to `config/initializers/shopify_app.rb`
521
- - *REMOVE* `config.per_user_tokens = [true|false]` this is no longer needed
522
- - *CHANGE* `config.session_repository = 'Shop'` To `config.shop_session_repository = 'Shop'`
523
- - *ADD (optional)* User Session Storage `config.user_session_repository = 'User'`
93
+ ### Overview
524
94
 
525
- ### Shop Model Changes (normally `app/models/shop.rb`)
526
- - *CHANGE* `include ShopifyApp::SessionStorage` to `include ShopifyApp::ShopSessionStorage`
95
+ [Quickstart](/docs/Quickstart.md)
527
96
 
528
- ### Changes to the @shop_session instance variable (normally in `app/controllers/*.rb`)
529
- - *CHANGE* if you are using shop sessions, `@shop_session` will need to be changed to `@current_shopify_session`.
97
+ [Troubleshooting](/docs/Troubleshooting.md)
530
98
 
531
- ### Changes to Rails `session`
532
- - *CHANGE* `session[:shopify]` is no longer set. Use `session[:user_id]` if your app uses user based tokens, or `session[:shop_id]` if your app uses shop based tokens.
99
+ [Upgrading](/docs/Upgrading.md)
533
100
 
534
- ### Changes to `ShopifyApp::LoginProtection`
535
- `ShopifyApp::LoginProtection`
101
+ [Shopify App](/docs/shopify_app)
102
+ * [Authentication](/docs/shopify_app/authentication.md)
103
+ * [Engine](/docs/shopify_app/engine.md)
104
+ * [Generators](/docs/shopify_app/generators.md)
105
+ * [ScriptTags](/docs/shopify_app/script-tags.md)
106
+ * [Session repository](/docs/shopify_app/session-repository.md)
107
+ * [Handling changes in access scopes](/docs/shopify_app/handling-access-scopes-changes.md)
108
+ * [Testing](/docs/shopify_app/testing.md)
109
+ * [Webhooks](/docs/shopify_app/webhooks.md)
536
110
 
537
- - CHANGE if you are using `ShopifyApp::LoginProtection#shopify_session` in your code, it will need to be
538
- changed to `ShopifyApp::LoginProtection#activate_shopify_session`
539
- - CHANGE if you are using `ShopifyApp::LoginProtection#clear_shop_session` in your code, it will need to be
540
- changed to `ShopifyApp::LoginProtection#clear_shopify_session`
111
+ ### Engine
541
112
 
542
- ### Notes
543
- You do not need a user model; a shop session is fine for most applications.
113
+ Mounting the Shopify App Rails Engine provides the following routes. These routes are configured to help install your application on shops and implement OAuth.
544
114
 
545
- Questions or problems?
546
- ----------------------
115
+ | Verb | Route | Action |
116
+ | ---: | :--- | :--- |
117
+ | `GET` | `/login` | Login |
118
+ | `POST` | `/login` | Login |
119
+ | `GET` | `/auth/shopify/callback` | OAuth redirect URI |
120
+ | `GET` | `/logout` | Logout |
121
+ | `POST` | `/webhooks/:type` | Webhook callback |
547
122
 
548
- - [Ask questions!](https://ecommerce.shopify.com/c/shopify-apis-and-technology)
549
- - [Read the docs!](https://help.shopify.com/api/guides)
550
- - And don't forget to check the [Changelog](https://github.com/Shopify/shopify_app/blob/master/CHANGELOG.md) too!
123
+ These routes are configurable. See the more detailed [*Engine*](/docs/shopify_app/engine.md) documentation to learn how you can customize the login URL or mount the Shopify App Rails engine at nested routes.
551
124
 
552
- Upgrading to 11.7.0
553
- ---------------------------
554
-
555
- ### Session storage method signature breaking change
556
- If you override `def self.store(auth_session)` method in your session storage model (e.g. Shop), the method signature has changed to `def self.store(auth_session, *args)` in order to support user-based token storage. Please update your method signature to include the second argument.
557
-
558
- Rails 6 Compatibility
559
- ---------------------
560
-
561
- ### Disable Webpacker
562
- If you are using sprockets in rails 6 or want to generate a shopify_app without webpacker run the install task by running
563
-
564
- ```
565
- SHOPIFY_APP_DISABLE_WEBPACKER=1 rails generate shopify_app
566
- ```
567
-
568
- and then in your ShopifyApp configuration block, add
569
-
570
- ```
571
- ShopifyApp.configure do |config|
572
- config.disable_webpacker = true
573
- end
574
- ```
575
-
576
- Upgrading from 8.6 to 9.0.0
577
- ---------------------------
578
-
579
- ### Configuration change
580
-
581
- Add an API version configuration in `config/initializers/shopify_app.rb`
582
- Set this to the version you want to run against by default. See [Shopify API docs](https://help.shopify.com/en/api/versioning) for versions available.
583
- ```ruby
584
- config.api_version = '2019-04'
585
- ```
586
-
587
- ### Session storage change
588
-
589
- You will need to add an `api_version` method to your session storage object. The default implementation for this is.
590
- ```ruby
591
- def api_version
592
- ShopifyApp.configuration.api_version
593
- end
594
- ```
595
-
596
- ### Generated file change
597
-
598
- `embedded_app.html.erb` the usage of `shop_session.url` needs to be changed to `shop_session.domain`
599
- ```erb
600
- <script type="text/javascript">
601
- ShopifyApp.init({
602
- apiKey: "<%= ShopifyApp.configuration.api_key %>",
603
-
604
- shopOrigin: "<%= "https://#{ @shop_session.url }" if @shop_session %>",
605
-
606
- debug: false,
607
- forceRedirect: true
608
- });
609
- </script>
610
- ```
611
- is changed to
612
- ```erb
613
- <script type="text/javascript">
614
- ShopifyApp.init({
615
- apiKey: "<%= ShopifyApp.configuration.api_key %>",
616
-
617
- shopOrigin: "<%= "https://#{ @shop_session.domain }" if @shop_session %>",
618
-
619
- debug: false,
620
- forceRedirect: true
621
- });
622
- </script>
623
- ```
125
+ To learn more about how this gem authenticates with Shopify, see [*Authentication*](/docs/shopify_app/authentication.md).
624
126
 
625
- ### ShopifyAPI changes
127
+ ### API Versioning
626
128
 
627
- You will need to also follow the ShopifyAPI [upgrade guide](https://github.com/Shopify/shopify_api/blob/master/README.md#-breaking-change-notice-for-version-700-) to ensure your app is ready to work with API versioning.
129
+ [Shopify's API is versioned](https://shopify.dev/concepts/about-apis/versioning). With Shopify App `v1.11.0`, the included Shopify API gem allows developers to specify and update the Shopify API version they want their app or service to use. The Shopify API gem also surfaces warnings to Rails apps about [deprecated endpoints, GraphQL fields and more](https://shopify.dev/concepts/about-apis/versioning#deprecation-practices).
628
130
 
629
- [dashboard]:https://partners.shopify.com
630
- [app-bridge]:https://help.shopify.com/en/api/embedded-apps/app-bridge
131
+ See the [Shopify API gem README](https://github.com/Shopify/shopify_api/) for more information.