Skip to content

Commit

Permalink
Create concern to authenticate deep links using Turbolinks
Browse files Browse the repository at this point in the history
  • Loading branch information
NabeelAhsen committed Dec 8, 2020
1 parent f541c62 commit 9336b4a
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
22 changes: 22 additions & 0 deletions app/controllers/concerns/shopify_app/ensure_authenticated_links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module ShopifyApp
module EnsureAuthenticatedLinks
extend ActiveSupport::Concern

included do
before_action :redirect_to_splash_page, if: :missing_expected_jwt?
end

private

def redirect_to_splash_page
splash_page_path = root_path(return_to: request.fullpath, shop: current_shopify_domain)
redirect_to(splash_page_path)
end

def missing_expected_jwt?
jwt_shopify_domain.blank?
end
end
end
22 changes: 22 additions & 0 deletions app/controllers/concerns/shopify_app/turbolinks_authenticated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module ShopifyApp
module TurbolinksAuthenticated
extend ActiveSupport::Concern

included do
before_action :redirect_to_splash_page, if: :missing_expected_jwt?
end

private

def redirect_to_splash_page
splash_page_path = root_path(return_to: request.fullpath, shop: current_shopify_domain)
redirect_to(splash_page_path)
end

def missing_expected_jwt?
jwt_shopify_domain.blank?
end
end
end
58 changes: 58 additions & 0 deletions test/controllers/concerns/ensure_authenticated_links_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'test_helper'

class EnsureAuthenticatedLinksTest < ActionController::TestCase
class TurbolinksTestController < ActionController::Base
include ShopifyApp::EnsureAuthenticatedLinks

def root
render(html: '<h1>Splash Page</ h1>')
end

def some_link
render(html: '<h1>Success</ h1>')
end

private

def jwt_shopify_domain
request.env['jwt.shopify_domain']
end

def current_shopify_domain
'test-shop.myshopify.com'
end
end

tests TurbolinksTestController

setup do
@shop_domain = 'test-shop.myshopify.com'

Rails.application.routes.draw do
root to: 'ensure_authenticated_links_test/turbolinks_test#root'
get '/some_link', to: 'ensure_authenticated_links_test/turbolinks_test#some_link'
end
end

teardown do
Rails.application.reload_routes!
end

test 'redirects to splash page with a return_to and shop param if no session token is present' do
get :some_link, params: { shop: @shop_domain }

expected_path = root_path(return_to: request.fullpath, shop: @shop_domain)

assert_redirected_to expected_path
end

test 'returns the requested resource if a valid session token exists' do
request.env['jwt.shopify_domain'] = @shop_domain

get :some_link, params: { shop: @shop_domain }

assert_response :ok
end
end
58 changes: 58 additions & 0 deletions test/controllers/concerns/turbolinks_authenticated_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'test_helper'

class TurbolinksAuthenticatedTest < ActionController::TestCase
class TurbolinksTestController < ActionController::Base
include ShopifyApp::TurbolinksAuthenticated

def root
render(html: '<h1>Splash Page</ h1>')
end

def some_link
render(html: '<h1>Success</ h1>')
end

private

def jwt_shopify_domain
request.env['jwt.shopify_domain']
end

def current_shopify_domain
'test-shop.myshopify.com'
end
end

tests TurbolinksTestController

setup do
@shop_domain = 'test-shop.myshopify.com'

Rails.application.routes.draw do
root to: 'turbolinks_authenticated_test/turbolinks_test#root'
get '/some_link', to: 'turbolinks_authenticated_test/turbolinks_test#some_link'
end
end

teardown do
Rails.application.reload_routes!
end

test 'redirects to splash page with a return_to and shop param if no session token is present' do
get :some_link, params: { shop: @shop_domain }

expected_path = root_path(return_to: request.fullpath, shop: @shop_domain)

assert_redirected_to expected_path
end

test 'returns the requested resource if a valid session token exists' do
request.env['jwt.shopify_domain'] = @shop_domain

get :some_link, params: { shop: @shop_domain }

assert_response :ok
end
end

0 comments on commit 9336b4a

Please sign in to comment.