Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/controllers/spree/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ def self.provides_callback_for(*providers)
providers.each do |provider|
class_eval <<-FUNCTION_DEFS, __FILE__, __LINE__ + 1
def #{provider}
pretty_provider_name = Spree::AuthenticationMethod.name_presentation(auth_hash['provider'])

if request.env['omniauth.error'].present?
flash[:error] = I18n.t('devise.omniauth_callbacks.failure', kind: auth_hash['provider'], reason: Spree.t(:user_was_not_valid))
flash[:error] = I18n.t('devise.omniauth_callbacks.failure', kind: pretty_provider_name, reason: Spree.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? and authentication.try(:user).present?
flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: auth_hash['provider'])
flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: pretty_provider_name)
sign_in_and_redirect :spree_user, authentication.user
elsif spree_current_user
spree_current_user.apply_omniauth(auth_hash)
Expand All @@ -28,11 +30,11 @@ def #{provider}
user = Spree::User.find_by_email(auth_hash['info']['email']) || Spree::User.new
user.apply_omniauth(auth_hash)
if user.save
flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: auth_hash['provider'])
flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: pretty_provider_name)
sign_in_and_redirect :spree_user, user
else
session[:omniauth] = auth_hash.except('extra')
flash[:notice] = Spree.t(:one_more_step, kind: auth_hash['provider'].capitalize)
flash[:notice] = Spree.t(:one_more_step, kind: pretty_provider_name)
redirect_to new_spree_user_registration_url
return
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/spree/authentication_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ def self.active_authentication_methods?
sc = sc.where.not(provider: user.user_authentications.pluck(:provider)) if user && !user.user_authentications.empty?
sc
}

def self.name_presentation(provider)
return '' unless provider
Spree.t("authentications.name_presentation.#{provider}")
end
end
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ en:
add_another_service: "Add another service to sign in with:"
authentications:
destroy: 'Successfully destroyed authentication method.'
name_presentation:
facebook: Facebook
google_oauth2: Google
github: Github
twitter: Twitter
amazon: Amazon
back_to_authentication_methods_list: "Back To Authentication Methods List"
edit_social_method: "Editing Social Authentication Method"
new_social_method: "New Authentication Method"
Expand Down