1

I'm trying to save the name of a Facebook user upon saving that user, but I don't seem to be able to. I have followed the guides on the devise github and the integration with Facebook works fine; the users email is saved as is to be expected. However, I can't figure out how to save the users name. Right now, I do this:

  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
    data = access_token['extra']['user_hash']
    if user = User.find_by_email(data["email"])
      user
    else # Create a user with a stub password.
      User.create(:email => data["email"], :name => data["name"], :password => Devise.friendly_token[0,20]) 
    end
  end

but that doesn't work. Am I doing something wrong?

Jasper Kennis
  • 3,225
  • 6
  • 41
  • 74

2 Answers2

4

Seems like Auth hash schema has changed a lot: https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
  data = access_token.extra.raw_info
  if user = User.where(:email => data.email).first
    user
  else
    # Create a user with a stub password. 
    user = User.create!(:username => data.username ? data.username : data.nickname , :email => data.email, :password => Devise.friendly_token[0,20])
  end
end
dbKooper
  • 1,035
  • 10
  • 17
0

The users name is stored in:

auth = request.env['omniauth.auth']  #I think this is what your access_token variable equates to.
auth['user_info']['name']

If that is not what you need, I suggest you inspect the contents of the access_token.

Gazler
  • 83,029
  • 18
  • 279
  • 245
  • Unfortunately, no. Request isn't accessible from within the method, and I found in [this stack overflow post](http://stackoverflow.com/questions/3580557/rails-3-using-devise-how-to-allow-someone-to-log-in-using-their-facebook-account) where an example is given using 'data["name"]', and looking at Facebooks' docs, "name" is supposed to be within '["extra"]["user_hash"]' – Jasper Kennis Sep 08 '11 at 20:17