Sha256: 2d579bff2c2d4418b97a7214dd63c0313be05a1020e74ba9ca2af496aa5ed271

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module ShopifyApp
  module SessionsController
    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?

        flash[:notice] = "Logged in"
        redirect_to return_address
      else
        flash[:error] = "Could not log in to Shopify store."
        redirect_to login_url
      end
    end

    def destroy
      session[:shopify] = nil
      session[:shopify_domain] = nil
      flash[:notice] = "Successfully logged out."
      redirect_to 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 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

3 entries across 3 versions & 1 rubygems

Version Path
shopify_app-6.4.2 lib/shopify_app/sessions_controller.rb
shopify_app-6.4.1 lib/shopify_app/sessions_controller.rb
shopify_app-6.4.0 lib/shopify_app/sessions_controller.rb