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
|