Skip to content

Commit

Permalink
Add ShopHost concern for fetching and saving host
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov committed Aug 6, 2021
1 parent 4a95665 commit b46ccc1
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module EnsureAuthenticatedLinks
private

def splash_page
splash_page_with_params(shop: current_shopify_domain, host: params[:host])
splash_page_with_params(shop: current_shopify_domain, host: @host)
end

def base_splash_page
Expand Down
37 changes: 37 additions & 0 deletions app/controllers/concerns/shopify_app/shop_host.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module ShopifyApp
module ShopHost
extend ActiveSupport::Concern

SHOP_HOST_COOKIE = :shop_host

included do
before_action :set_shop_host
end

def set_shop_host
@host = fetch_host_from_params
@host ||= fetch_host_from_cookies
save_shop_host(@host) if @host
@host
end

private

def fetch_host_from_params
params[:host]
end

def fetch_host_from_cookies
cookies[SHOP_HOST_COOKIE]
end

def save_shop_host(host)
cookies[SHOP_HOST_COOKIE] = {
value: host,
expires: 1.day.from_now,
}
end
end
end
1 change: 1 addition & 0 deletions app/controllers/shopify_app/callback_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module ShopifyApp
# Performs login after OAuth completes
class CallbackController < ActionController::Base
include ShopifyApp::ShopHost
include ShopifyApp::LoginProtection

def callback
Expand Down
1 change: 1 addition & 0 deletions app/controllers/shopify_app/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
module ShopifyApp
class SessionsController < ActionController::Base
include ShopifyApp::ShopHost
include ShopifyApp::LoginProtection

layout false, only: :new
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
# frozen_string_literal: true

class HomeController < AuthenticatedController
include ShopifyApp::ShopHost
include ShopifyApp::ShopAccessScopesVerification

before_action :set_host

def index
@products = ShopifyAPI::Product.find(:all, params: { limit: 10 })
@webhooks = ShopifyAPI::Webhook.find(:all)
end

private

def set_host
@host = params[:host]
end
end
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

class HomeController < ApplicationController
include ShopifyApp::ShopHost
include ShopifyApp::EmbeddedApp
include ShopifyApp::RequireKnownShop
include ShopifyApp::ShopAccessScopesVerification

def index
@shop_origin = current_shopify_domain
@host = params[:host]
end
end
2 changes: 1 addition & 1 deletion lib/shopify_app/controller_concerns/login_protection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def jwt_shopify_user_id
end

def host
return params[:host] if params[:host].present?
return @host if @host.present?

raise ShopifyHostNotFound
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

def root
Expand Down

0 comments on commit b46ccc1

Please sign in to comment.