Skip to content

Commit 1bb51d2

Browse files
authored
Merge pull request #432 from Shopify/add_after_authenticate_job_option
Add after authenticate job option
2 parents 83d9811 + 3939188 commit 1bb51d2

File tree

12 files changed

+181
-1
lines changed

12 files changed

+181
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
7.4.0
2+
-----
3+
* Add an after_authenticate job which will be run once the shop is authenticated. [[#431]](https://github.com/Shopify/shopify_app/pull/432)
4+
15
7.3.0
26
-----
37
* Bump required omniauth-shopify-oauth2 version to 1.2.0.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Table of Contents
2727
* [**Managing Api Keys**](#managing-api-keys)
2828
* [**WebhooksManager**](#webhooksmanager)
2929
* [**ScripttagsManager**](#scripttagsmanager)
30+
* [**AfterAuthenticate Job**](#afterauthenticate-job)
3031
* [**ShopifyApp::SessionRepository**](#shopifyappsessionrepository)
3132
* [**AuthenticatedController**](#authenticatedcontroller)
3233
* [**AppProxyVerification**](#appproxyverification)
@@ -298,6 +299,31 @@ Scripttags are created in the same way as the Webhooks, with a background job wh
298299

299300
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.
300301

302+
AfterAuthenticate Job
303+
---------------------
304+
305+
If your app needs to perform specific actions after it is installed 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:
306+
307+
```ruby
308+
ShopifyApp.configure do |config|
309+
config.add_after_authenticate_job = { job: Shopify::AfterAuthenticateJob }
310+
end
311+
```
312+
313+
If you need the job to run synchronously add the `inline` flag:
314+
315+
```ruby
316+
ShopifyApp.configure do |config|
317+
config.add_after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: true }
318+
end
319+
```
320+
321+
We've also provided a generator which creates a skeleton job and updates the initializer for you:
322+
323+
```
324+
bin/rails g shopify_app:add_after_authenticate_job
325+
```
326+
301327
ShopifyApp::SessionRepository
302328
-----------------------------
303329

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'rails/generators/base'
2+
3+
module ShopifyApp
4+
module Generators
5+
class AddAfterAuthenticateJobGenerator < Rails::Generators::Base
6+
source_root File.expand_path('../templates', __FILE__)
7+
8+
hook_for :test_framework, as: :job, in: :rails do |instance, generator|
9+
instance.invoke generator, [ instance.send(:job_file_name) ]
10+
end
11+
12+
def init_after_authenticate_config
13+
initializer = load_initializer
14+
15+
after_authenticate_job_config = " config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: false }\n"
16+
17+
inject_into_file(
18+
'config/initializers/shopify_app.rb',
19+
after_authenticate_job_config,
20+
before: 'end'
21+
)
22+
23+
unless initializer.include?(after_authenticate_job_config)
24+
shell.say "Error adding after_authneticate_job to config. Add this line manually: #{after_authenticate_job_config}", :red
25+
end
26+
end
27+
28+
def add_after_authenticate_job
29+
template 'after_authenticate_job.rb', "app/jobs/#{job_file_name}_job.rb"
30+
end
31+
32+
private
33+
34+
def load_initializer
35+
File.read(File.join(destination_root, 'config/initializers/shopify_app.rb'))
36+
end
37+
38+
def job_file_name
39+
'shopify/after_authenticate'
40+
end
41+
end
42+
end
43+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Shopify
2+
class AfterAuthenticateJob < ActiveJob::Base
3+
def perform(shop_domain:)
4+
shop = Shop.find_by(shopify_domain: shop_domain)
5+
6+
shop.with_shopify_session do
7+
end
8+
end
9+
end
10+
end

lib/generators/shopify_app/install/templates/shopify_app.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
config.secret = "<%= @secret %>"
55
config.scope = "<%= @scope %>"
66
config.embedded_app = <%= embedded_app? %>
7+
config.after_authenticate_job = false
78
end

lib/shopify_app/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Configuration
1212
alias_method :embedded_app?, :embedded_app
1313
attr_accessor :webhooks
1414
attr_accessor :scripttags
15+
attr_accessor :after_authenticate_job
1516

1617
# customise ActiveJob queue names
1718
attr_accessor :scripttags_manager_queue_name

lib/shopify_app/sessions_concern.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def callback
2020
login_shop
2121
install_webhooks
2222
install_scripttags
23+
perform_after_authenticate_job
2324

2425
redirect_to return_address
2526
else
@@ -87,5 +88,16 @@ def return_address
8788
session.delete(:return_to) || main_app.root_url
8889
end
8990

91+
def perform_after_authenticate_job
92+
config = ShopifyApp.configuration.after_authenticate_job
93+
94+
return unless config && config[:job].present?
95+
96+
if config[:inline] == true
97+
config[:job].perform_now(shop_domain: session[:shopify_domain])
98+
else
99+
config[:job].perform_later(shop_domain: session[:shopify_domain])
100+
end
101+
end
90102
end
91103
end

lib/shopify_app/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ShopifyApp
2-
VERSION = '7.3.0'
2+
VERSION = '7.4.0'
33
end

test/controllers/sessions_controller_test.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
require 'test_helper'
22

3+
module Shopify
4+
class AfterAuthenticateJob < ActiveJob::Base
5+
def perform; end
6+
end
7+
end
8+
39
module ShopifyApp
410
class SessionsControllerTest < ActionController::TestCase
511

@@ -140,6 +146,50 @@ class SessionsControllerTest < ActionController::TestCase
140146
assert_equal 'Cerrar sesión', flash[:notice]
141147
end
142148

149+
test "#callback calls #perform_after_authenticate_job and performs inline when inline is true" do
150+
ShopifyApp.configure do |config|
151+
config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: true }
152+
end
153+
154+
Shopify::AfterAuthenticateJob.expects(:perform_now)
155+
156+
mock_shopify_omniauth
157+
get :callback, params: { shop: 'shop' }
158+
end
159+
160+
test "#callback calls #perform_after_authenticate_job and performs asynchronous when inline isn't true" do
161+
ShopifyApp.configure do |config|
162+
config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: false }
163+
end
164+
165+
Shopify::AfterAuthenticateJob.expects(:perform_later)
166+
167+
mock_shopify_omniauth
168+
get :callback, params: { shop: 'shop' }
169+
end
170+
171+
test "#callback doesn't call #perform_after_authenticate_job if job is nil" do
172+
ShopifyApp.configure do |config|
173+
config.after_authenticate_job = { job: nil, inline: false }
174+
end
175+
176+
Shopify::AfterAuthenticateJob.expects(:perform_later).never
177+
178+
mock_shopify_omniauth
179+
get :callback, params: { shop: 'shop' }
180+
end
181+
182+
test "#callback calls #perform_after_authenticate_job and performs async if inline isn't present" do
183+
ShopifyApp.configure do |config|
184+
config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob }
185+
end
186+
187+
Shopify::AfterAuthenticateJob.expects(:perform_later)
188+
189+
mock_shopify_omniauth
190+
get :callback, params: { shop: 'shop' }
191+
end
192+
143193
private
144194

145195
def mock_shopify_omniauth
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'test_helper'
2+
require 'generators/shopify_app/add_after_authenticate_job/add_after_authenticate_job_generator'
3+
4+
class AddAfterAuthenticateJobGeneratorTest < Rails::Generators::TestCase
5+
tests ShopifyApp::Generators::AddAfterAuthenticateJobGenerator
6+
destination File.expand_path("../tmp", File.dirname(__FILE__))
7+
8+
setup do
9+
prepare_destination
10+
end
11+
12+
test 'adds enable_after_authenticate_actions config' do
13+
provide_existing_initializer_file
14+
15+
run_generator
16+
17+
assert_file "config/initializers/shopify_app.rb" do |config|
18+
assert_match 'config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: false }', config
19+
end
20+
end
21+
22+
test "adds the after_authenticate job" do
23+
provide_existing_initializer_file
24+
25+
run_generator
26+
27+
assert_directory "app/jobs/shopify"
28+
assert_file "app/jobs/shopify/after_authenticate_job.rb"
29+
end
30+
end

0 commit comments

Comments
 (0)