I've been working with the Facebook Omniauth and Devise but have run into an issue where the returned hash does not contain any of the information I'm requesting in my info fields. I'm just trying to get Facebook to return the first and last names with the email if it's available separately so I can create new users with that information. I don't think I need to set up specific permissions with Facebook to return these fields (as you do with some info_fields that require verification).
I am using Rails 4.2.6. This is the returned hash:
#<OmniAuth::AuthHash credentials=#<OmniAuth::AuthHash
expires=true expires_at=1486542909
token="EAAXYor9I0XgBADbIaZC99Lu0xGAdVVSXSyZC1TsZCHcZCi0kiWPdZCHA0DsLait7zn1cSpEsbc1mViWvFxZAPraz2Hdyfyn2pfonpeDlhP6Nqv7RiGNpDQ1Bxxm81LHwoPHMIbag95pksGZChcQ7YMrk8eUuQNkgScZD">
extra=#<OmniAuth::AuthHash
raw_info=#<OmniAuth::AuthHash id="xxxxxxxxxx" name="xxxxxxxx">>
info=#<OmniAuth::AuthHash::InfoHash
image="http://graph.facebook.com/xxxxxxxxxx/picture?type=square"
name="xxxxxxxxxx">
provider="facebook"
uid="xxxxxxxxxx">
Most of the solutions for similar questions have involved something in devise.rb, which looks like this for me:
config.omniauth :facebook, "xxxIDxxx", "xxxSECRETxxx", scope: 'email,public_profile', info_fields: 'email, first_name, last_name'
My CallBacksController:
class CallbacksController < Devise::OmniauthCallbacksController
def all
@user = User.from_omniauth(request.env["omniauth.auth"])
@request = request.env["omniauth.auth"]
if @user.persisted?
flash.notice = "You have successfully signed in!"
sign_in_and_redirect @user
else
session["devise.user_attributes"] = @user.attributes
redirect_to new_user_registration_path
end
end
alias_method :facebook, :all
end
My User.rb:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]
has_many :posts
def self.from_omniauth(auth)
puts auth
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email || ""
user.password = Devise.friendly_token[0,20]
end
end
And these are the related gems I'm using:
gem 'geocoder'
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'
This has been driving me crazy for the past few days and I'm stuck. Any suggestions would be greatly appreciated. If there are things that might be related that I accidentally left out, let me know and I'd be glad to post them. Thanks!