forked from Shopify/shopify_app
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ShopHost concern for fetching and saving host
- Loading branch information
1 parent
4a95665
commit b46ccc1
Showing
8 changed files
with
44 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 1 addition & 8 deletions
9
lib/generators/shopify_app/home_controller/templates/home_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
2 changes: 1 addition & 1 deletion
2
lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters