Sha256: 0b9937b38d4ba9407b110e6a183cf6628854bf51501f13b6490db33dba244891

Contents?: true

Size: 1008 Bytes

Versions: 2

Compression:

Stored size: 1008 Bytes

Contents

module ShopifyApp::LoginProtection
  extend ActiveSupport::Concern

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

  def shopify_session
    if shop_session
      begin
        # session[:shopify] set in LoginController#show
        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 ||= ShopifySessionRepository.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]
      redirect_to_login
    end
  end

  protected

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shopify_app-5.0.2 lib/shopify_app/login_protection.rb
shopify_app-5.0.1 lib/shopify_app/login_protection.rb