Sha256: 4870d6992b703fa67285043ca289b418699b0eabc298aa528a29696181134e25

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module ShopifyApp
  module EnsureInstalled
    extend ActiveSupport::Concern
    include ShopifyApp::RedirectForEmbedded

    included do
      if ancestors.include?(ShopifyApp::LoginProtection)
        message = <<~EOS
          We detected the use of incompatible concerns (EnsureInstalled and LoginProtection) in #{name},
          which may lead to unpredictable behavior. In a future release of this library this will raise an error.
        EOS

        ShopifyApp::Logger.deprecated(message, "22.0.0")
      end

      before_action :check_shop_domain
      before_action :check_shop_known
    end

    def current_shopify_domain
      if params[:shop].blank?
        ShopifyApp::Logger.info("Could not identify installed store from current_shopify_domain")
        return
      end

      @shopify_domain ||= ShopifyApp::Utils.sanitize_shop_domain(params[:shop])
      ShopifyApp::Logger.info("Installed store:  #{@shopify_domain} - deduced from Shopify Admin params")
      @shopify_domain
    end

    private

    def check_shop_domain
      redirect_to(ShopifyApp.configuration.login_url) unless current_shopify_domain
    end

    def check_shop_known
      @shop = SessionRepository.retrieve_shop_session_by_shopify_domain(current_shopify_domain)
      unless @shop
        if embedded_param?
          redirect_for_embedded
        else
          redirect_to(shop_login)
        end
      end
    end

    def shop_login
      url = URI(ShopifyApp.configuration.login_url)

      url.query = URI.encode_www_form(
        shop: params[:shop],
        host: params[:host],
        return_to: request.fullpath,
      )

      url.to_s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shopify_app-21.3.1 app/controllers/concerns/shopify_app/ensure_installed.rb
shopify_app-21.3.0 app/controllers/concerns/shopify_app/ensure_installed.rb