Sha256: 515ac3ea72dc7b3cfbec26bd3f323831b723d348c5e6bf13d4e74e5c4a7a314b

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

class SessionsController < ApplicationController
  def new
    authenticate if params[:shop].present?
  end

  def create
    authenticate
  end
  
  def show
    if response = request.env['omniauth.auth']
      sess = ShopifyAPI::Session.new(params[:shop], response['credentials']['token'])
      session[:shopify] = ShopifySessionRepository.store(sess)
      flash[:notice] = "Logged in"
      redirect_to return_address
    else
      flash[:error] = "Could not log in to Shopify store."
      redirect_to :action => 'new'
    end
  end
  
  def destroy
    session[:shopify] = nil
    flash[:notice] = "Successfully logged out."
    
    redirect_to :action => 'new'
  end
  
  protected
  
  def authenticate
    if shop_name = sanitize_shop_param(params)
      redirect_to "/auth/shopify?shop=#{shop_name}"
    else
      redirect_to return_address
    end
  end
  
  def return_address
    session[:return_to] || root_url
  end
  
  def sanitize_shop_param(params)
    return unless params[:shop].present?
    name = params[:shop].to_s.strip
    name += '.myshopify.com' if !name.include?("myshopify.com") && !name.include?(".")
    name.sub!(%r|https?://|, '')

    u = URI("http://#{name}")
    u.host.ends_with?(".myshopify.com") ? u.host : nil
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
shopify_app-5.0.2 lib/generators/shopify_app/templates/app/controllers/sessions_controller.rb
shopify_app-5.0.1 lib/generators/shopify_app/templates/app/controllers/sessions_controller.rb
shopify_app_lrb3-5.0.0 lib/generators/shopify_app/templates/app/controllers/sessions_controller.rb
shopify_app-5.0.0 lib/generators/shopify_app/templates/app/controllers/sessions_controller.rb