Class: Spree::OmniauthCallbacksController

Inherits:
Devise::OmniauthCallbacksController
  • Object
show all
Includes:
Core::ControllerHelpers, Core::CurrentOrder
Defined in:
app/controllers/spree/omniauth_callbacks_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.provides_callback_for(*providers) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/spree/omniauth_callbacks_controller.rb', line 5

def self.provides_callback_for(*providers)
  providers.each do |provider|
    class_eval %Q{
      def #{provider}
        if request.env["omniauth.error"].present?
          flash[:error] = t("devise.omniauth_callbacks.failure", :kind => auth_hash['provider'], :reason => t(:user_was_not_valid))
          redirect_back_or_default(root_url)
          return
        end

        authentication = Spree::UserAuthentication.find_by_provider_and_uid(auth_hash['provider'], auth_hash['uid'])

        if authentication.present?
          flash[:notice] = "Signed in successfully"
          sign_in_and_redirect :user, authentication.user
        elsif current_user
          current_user.user_authentications.create!(:provider => auth_hash['provider'], :uid => auth_hash['uid'])
          flash[:notice] = "Authentication successful."
          redirect_back_or_default(account_url)
        else
          user = Spree::User.find_by_email(auth_hash['info']['email']) || Spree::User.new
          user.apply_omniauth(auth_hash)
          if user.save
            flash[:notice] = "Signed in successfully."
            sign_in_and_redirect :user, user
          else
            session[:omniauth] = auth_hash.except('extra')
            flash[:notice] = t(:one_more_step, :kind => auth_hash['provider'].capitalize)
            redirect_to new_user_registration_url
          end
        end

        if current_order
          user = current_user if current_user
          current_order.associate_user!(user)
          session[:guest_token] = nil
        end
      end
    }
  end
end

Instance Method Details

#auth_hashObject



60
61
62
# File 'app/controllers/spree/omniauth_callbacks_controller.rb', line 60

def auth_hash
  request.env["omniauth.auth"]
end

#failureObject



51
52
53
54
# File 'app/controllers/spree/omniauth_callbacks_controller.rb', line 51

def failure
  set_flash_message :alert, :failure, :kind => failed_strategy.name.to_s.humanize, :reason => failure_message
  redirect_to spree.
end

#passthruObject



56
57
58
# File 'app/controllers/spree/omniauth_callbacks_controller.rb', line 56

def passthru
  render :file => "#{Rails.root}/public/404", :formats => [:html], :status => 404, :layout => false
end