Ho cercato per giorni di impostare il mio Omniauth per facebook Non so cosa sto sbagliando.Ruby on Rails Omniauth facebook non restituisce email
Non riesco a ottenere l'e-mail dell'utente. L'hash restituita contiene solo il "nome" e il "uid" nemmeno "first_name" e "cognome"
devise.rb:
config.omniauth :facebook, "KEY", "SECRET"
omniauth_callbacks_controller.rb:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
logger.info request.env["omniauth.auth"]
@user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect @user
end
end
utente .RB:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
has_many :authentications
def self.from_omniauth(auth)
logger.info auth
user = where(email: auth.info.email).first
if(user != nil)
user.authentications.where(provider: auth.provider, uid: auth.uid).first_or_create do |l|
user.authentications.create!(user_id: user.id,
provider: auth.provider,
uid: auth.uid)
end
else
user = User.create!(email: auth.info.email,
password: Devise.friendly_token[0,20],
first_name: auth.info.first_name,
last_name: auth.info.last_name)
user.authentications.create!(user_id: user.id,
provider: auth.provider,
uid: auth.uid)
end
user
end
end
registrations_controller.rb:
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
end
end
routes.rb:
devise_for :users, :controllers => { registrations: 'registrations', omniauth_callbacks: 'omniauth_callbacks' }
restituito hash:
#<OmniAuth::AuthHash credentials=#<OmniAuth::AuthHash expires=true expires_at=1444504014 token="TOKEN">
extra=#<OmniAuth::AuthHash raw_info=#<OmniAuth::AuthHash id="1506781179612589" name="Ayman Salah">> info=#
<OmniAuth::AuthHash::InfoHash image="http://graph.facebook.com/1506781179612589/picture" name="Ayman Salah"> provider="facebook" uid="1506781179612589">
Facebook ora consente agli utenti di non condividere il proprio indirizzo email quando si utilizza il login oauth. Questo creerà un'eccezione perché Devise richiede l'indirizzo email nel modello utente. – Red
Grazie mille! –
@SankalpSingha Sei più che benvenuto. –