我正在使用spree社交gem,其中需要进行身份验证。但是,我想从谷歌获取用户名,但是auth_hash [' info']似乎只有电子邮件和图片
我在以下装饰器中只更改了一行
user.name = auth_hash['info']['name']
Spree::OmniauthCallbacksController.class_eval do
include Spree::Core::ControllerHelpers::Common
include Spree::Core::ControllerHelpers::Order
include Spree::Core::ControllerHelpers::Auth
include Spree::Core::ControllerHelpers::Store
def self.provides_callback_for(*providers)
providers.each do |provider|
class_eval <<-FUNCTION_DEFS, __FILE__, __LINE__ + 1
def #{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))
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'])
byebug
sign_in_and_redirect :spree_user, authentication.user
elsif spree_current_user
spree_current_user.apply_omniauth(auth_hash)
spree_current_user.save!
flash[:notice] = I18n.t('devise.sessions.signed_in')
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)
byebug
# custom code to add user name
user.name = auth_hash['info']['name']
if user.save
flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: auth_hash['provider'])
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)
redirect_to new_spree_user_registration_url
return
end
end
if current_order
user = spree_current_user || authentication.user
current_order.associate_user!(user)
session[:guest_token] = nil
end
end
FUNCTION_DEFS
end
end
SpreeSocial::OAUTH_PROVIDERS.each do |provider|
provides_callback_for provider[1].to_sym
end
def failure
set_flash_message :alert, :failure, kind: failed_strategy.name.to_s.humanize, reason: failure_message
redirect_to spree.login_path
end
def passthru
render file: "#{Rails.root}/public/404", formats: [:html], status: 404, layout: false
end
def auth_hash
request.env['omniauth.auth']
end
end
我需要在Google应用中更改权限吗?这个名字会出现吗?
答案 0 :(得分:0)
您必须启用google apps API下列出的联系人API。