Sha256: 5c5b6a415e7ac5ff578c2d6a1c378f91e3595b50cb4fde2b25e5895a36e1dd32

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

module ShopifyApp
  module LoginProtection
    extend ActiveSupport::Concern

    included do
      rescue_from ActiveResource::UnauthorizedAccess, :with => :close_session
    end

    def shopify_session
      if shop_session
        begin
          ShopifyAPI::Base.activate_session(shop_session)
          yield
        ensure
          ShopifyAPI::Base.clear_session
        end
      else
        redirect_to_login
      end
    end

    def shop_session
      return unless session[:shopify]
      @shop_session ||= ShopifyApp::SessionRepository.retrieve(session[:shopify])
    end

    def login_again_if_different_shop
      if shop_session && params[:shop] && params[:shop].is_a?(String) && shop_session.url != params[:shop]
        session[:shopify] = nil
        session[:shopify_domain] = nil
        redirect_to_login
      end
    end

    protected

    def redirect_to_login
      if request.xhr?
        head :unauthorized
      else
        session[:return_to] = request.fullpath if request.get?
        redirect_to login_path(shop: params[:shop])
      end
    end

    def close_session
      session[:shopify] = nil
      session[:shopify_domain] = nil
      redirect_to login_path
    end

    def login_path(params = {})
      main_app.login_path(params)
    rescue NoMethodError => e
      shopify_app.login_path(params)
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
shopify_app-6.4.2 lib/shopify_app/login_protection.rb
shopify_app-6.4.1 lib/shopify_app/login_protection.rb
shopify_app-6.4.0 lib/shopify_app/login_protection.rb
shopify_app-6.3.0 lib/shopify_app/login_protection.rb
shopify_app-6.2.1 lib/shopify_app/login_protection.rb
shopify_app-6.2.0 lib/shopify_app/login_protection.rb