Sha256: 9fbeb8c611d1dee95ce5783a03c8d40d325707645cdb1088af4937dac513dcf9

Contents?: true

Size: 1.64 KB

Versions: 9

Compression:

Stored size: 1.64 KB

Contents

module ShopifyApp
  module SessionsConcern
    extend ActiveSupport::Concern

    def new
      authenticate if params[:shop].present?
    end

    def create
      authenticate
    end

    def callback
      if response = request.env['omniauth.auth']
        shop_name = response.uid
        token = response['credentials']['token']

        sess = ShopifyAPI::Session.new(shop_name, token)
        session[:shopify] = ShopifyApp::SessionRepository.store(sess)
        session[:shopify_domain] = shop_name

        WebhooksManager.queue(shop_name, token) if ShopifyApp.configuration.has_webhooks?
        ScripttagsManager.queue(shop_name, token) if ShopifyApp.configuration.has_scripttags?

        flash[:notice] = I18n.t('.logged_in')
        redirect_to_with_fallback return_address
      else
        flash[:error] = I18n.t('could_not_log_in')
        redirect_to_with_fallback login_url
      end
    end

    def destroy
      session[:shopify] = nil
      session[:shopify_domain] = nil
      flash[:notice] = I18n.t('.logged_out')
      redirect_to_with_fallback login_url
    end

    protected

    def authenticate
      if shop_name = sanitize_shop_param(params)
        fullpage_redirect_to "#{main_app.root_path}auth/shopify?shop=#{shop_name}"
      else
        redirect_to_with_fallback return_address
      end
    end

    def return_address
      session.delete(:return_to) || main_app.root_url
    end

    def sanitized_shop_name
      @sanitized_shop_name ||= sanitize_shop_param(params)
    end

    def sanitize_shop_param(params)
      return unless params[:shop].present?
      ShopifyApp::Utils.sanitize_shop_domain(params[:shop])
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
shopify_app-7.0.10 lib/shopify_app/sessions_concern.rb
shopify_app-7.0.9 lib/shopify_app/sessions_concern.rb
shopify_app-7.0.8 lib/shopify_app/sessions_concern.rb
shopify_app-7.0.7 lib/shopify_app/sessions_concern.rb
shopify_app-7.0.6 lib/shopify_app/sessions_concern.rb
shopify_app-7.0.5 lib/shopify_app/sessions_concern.rb
shopify_app-7.0.4 lib/shopify_app/sessions_concern.rb
shopify_app-7.0.2 lib/shopify_app/sessions_concern.rb
shopify_app-7.0.1 lib/shopify_app/sessions_concern.rb