Joe Morano
Joe Morano

Reputation: 1895

How can I translate Facebook user data into records on my database?

I'm using the omniauth-facebook gem with devise to set up Facebook login on my website. I'm at the point of importing Facebook user data into my database, but I'm having some weird issues.

Here's my method used to import the data:

#User.rb

def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
    user.email = auth.info.email.to_s
    user.password = Devise.friendly_token[0,20]
    user.username = auth.info.name
  end
end

The record I get back has a user.username equal to the Facebook email, and the user.email is nil. auth.info.name (which is actually the email address) is the only piece of Facebook user data I'm actually able to recover. Everything else comes back nil.

Has anyone else encountered this?

Upvotes: 1

Views: 127

Answers (1)

Maciej Małecki
Maciej Małecki

Reputation: 2745

Why are you using user instead of member inside of the first_or_create block? Is it a typo while creating a code snippet?

Anyway, I believe that the issue is for users that exists in database. In such case block that is passed to the first_or_create method isn't evaluated (yield is only for new records) so fields are not updated.

Make sure that you specified those fields (or didn't specified only name) in omniauth-facebook configuration: https://github.com/mkdynamic/omniauth-facebook. It returns name and email by default.

You should also take a look at the great RailsCasts about Facebook Authentication.

Upvotes: 2

Related Questions