Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ShopHost concern for fetching and saving host #1360

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
18.1.0 (Jan 28, 2022)
----------
* Support Rails 7 [#1354](https://github.com/Shopify/shopify_app/pull/1354)
* Fix webhooks handling in Ruby 3 [#1342](https://github.com/Shopify/shopify_app/pull/1342)
* Fix webhooks handling in Ruby 3 [#1342](https://github.com/Shopify/shopify_app/pull/1342)
* Update to Ruby 3 and drop support to Ruby 2.5 [#1359](https://github.com/Shopify/shopify_app/pull/1359)
* Add ShopHost concern for fetching and saving host [#1360](https://github.com/Shopify/shopify_app/pull/1360)

18.0.4 (Jan 27, 2022)
----------
Expand All @@ -28,7 +29,7 @@

18.0.0 (May 3, 2021)
----------
* Support OmniAuth 2.x
* Support OmniAuth 2.x
* If your app has custom OmniAuth configuration, please refer to the [OmniAuth 2.0 upgrade guide](https://github.com/omniauth/omniauth/wiki/Upgrading-to-2.0).
* Support App Bridge version 2.x in the Embedded App layout. [#1241](https://github.com/Shopify/shopify_app/pull/1241)

Expand Down Expand Up @@ -58,7 +59,7 @@

17.0.4 (January 25, 2021)
----------
* Redirect user to login page if shopify domain is not found in the `EnsureAuthenticatedLinks` concern [#1158](https://github.com/Shopify/shopify_app/pull/1158)
* Redirect user to login page if shopify domain is not found in the `EnsureAuthenticatedLinks` concern [#1158](https://github.com/Shopify/shopify_app/pull/1158)

17.0.3 (January 22, 2021)
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def splash_page
splash_page_with_params(
return_to: request.fullpath,
shop: current_shopify_domain,
host: params[:host]
host: @host
)
end

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
24 changes: 20 additions & 4 deletions docs/shopify_app/authentication.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Authentication

The Shopify App gem implements [OAuth 2.0](https://shopify.dev/tutorials/authenticate-with-oauth) to get [access tokens](https://shopify.dev/concepts/about-apis/authentication#api-access-modes). These are used to authenticate requests made by the app to the Shopify API.
The Shopify App gem implements [OAuth 2.0](https://shopify.dev/tutorials/authenticate-with-oauth) to get [access tokens](https://shopify.dev/concepts/about-apis/authentication#api-access-modes). These are used to authenticate requests made by the app to the Shopify API.

By default, the gem generates an embedded app frontend that uses [Shopify App Bridge](https://shopify.dev/tools/app-bridge) to fetch [session tokens](https://shopify.dev/concepts/apps/building-embedded-apps-using-session-tokens). Session tokens are used by the embedded app to make authenticated requests to the app backend.
By default, the gem generates an embedded app frontend that uses [Shopify App Bridge](https://shopify.dev/tools/app-bridge) to fetch [session tokens](https://shopify.dev/concepts/apps/building-embedded-apps-using-session-tokens). Session tokens are used by the embedded app to make authenticated requests to the app backend.

See [*Authenticate an embedded app using session tokens*](https://shopify.dev/tutorials/authenticate-your-app-using-session-tokens) to learn more.
See [*Authenticate an embedded app using session tokens*](https://shopify.dev/tutorials/authenticate-your-app-using-session-tokens) to learn more.

> ⚠️ Be sure you understand the differences between the types of authentication schemes before reading this guide.
Expand Down Expand Up @@ -121,4 +121,20 @@ class AuthenticatedController < ApplicationController
end
```

See [Authenticate server-side rendered embedded apps using Rails and Turbolinks](https://shopify.dev/tutorials/authenticate-server-side-rendered-embedded-apps-using-rails-and-turbolinks) for more information.
See [Authenticate server-side rendered embedded apps using Rails and Turbolinks](https://shopify.dev/tutorials/authenticate-server-side-rendered-embedded-apps-using-rails-and-turbolinks) for more information.

### `ShopifyApp::ShopHost`

The [`ShopifyApp::ShopHost`](/app/controllers/concerns/shopify_app/shop_host.rb) concern handles fetching and caching `host` param in App Bridge 2.0 apps.

Include this concern in yours app's `SplashPageController` and `AuthenticatedController` if your app uses App Bridge 2.0. It adds `before_action` that sets `@host` variable from params or cookies and saves existing host into cookies. If host is missing in both params and cookies then `ShopifyHostNotFound` exception is raised.

*Example:*

```rb
class AuthenticatedController < ApplicationController
include ShopifyApp::ShopHost
include ShopifyApp::EnsureAuthenticatedLinks
include ShopifyApp::Authenticated
end
```
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