0

I'm tryig to get the user address from facebook with Omniauth but did not work.

i added their address on update callback after login.

If i removed their address from omniauth the app did not update their address.

Someone have any idea how to get their address and why the app did not edit and update their address after the login?

thank's

def omniauth_callback
    auth_hash = request.env['omniauth.auth']



    user = User.find_by_uid(auth_hash[:uid])
    if user.nil? && auth_hash[:info][:email]
      user = User.find_by_email(auth_hash[:info][:email])
    end

    if user.nil?
      email_domain = ''
      email_domain = '@facebook.com' if auth_hash[:provider] == 'facebook'
      user = User.new(email: auth_hash[:info][:email] || auth_hash[:info][:nickname] + email_domain, name: auth_hash[:info][:first_name] || '', surname: auth_hash[:info][:last_name] || '', gender: 'I')

      user.password_digest = ''
      user.save!(validate: false)
    end

    user.update_attribute(:login_at, Time.zone.now)
    user.update_attribute(:address)
    user.update_attribute(:neighborhood)
     user.update_attribute(:postal_code)
    user.update_attribute(:ip_address, request.remote_ip)
    user.update_attribute(:provider, auth_hash[:provider])
    user.update_attribute(:uid, auth_hash[:uid])
    user.update_attribute(:oauth_token, auth_hash[:credentials][:token])
    user.update_attribute(:oauth_expires_at, Time.at(auth_hash[:credentials][:expires_at]))

    cookies[:auth_token] = { value: user.oauth_token, expires: user.oauth_expires_at}
    redirect_to root_url
  end
  • Possible duplicate of [Ruby on Rails Omniauth facebook doesn't return email](http://stackoverflow.com/questions/31954540/ruby-on-rails-omniauth-facebook-doesnt-return-email) – OneNeptune Jan 06 '17 at 00:12
  • @OneNeptune, no. because is not address –  Jan 06 '17 at 00:14
  • Is the problem that Facebook is not providing the email, or that your code is not saving the email that is provided? – Ryenski Jan 06 '17 at 03:34
  • 1
    Are you aware that each `update_attribute` results in an individual write to the database? You've got a lot of unnecessary database activity going on here. Also you've got several `update_attribute` methods that only have one argument. That will probably raise an ArgumentError, which may be your problem. – Ryenski Jan 06 '17 at 03:36

1 Answers1

0

One reason your code will not work is because this

user.update_attribute(:address)

Doesn't do anything - except raise an error. You have to pass a value into update_attribute as well as specify the field.

Also as @mysmallidea points out, you'd be better advised to use update as that will allow you to update multiple fields in one database action.

If present, the address data will be within the auth_hash. So I suggest that you first work out the structure of that hash. In your development environment, add the following:

Rails.logger.info auth_hash.inspect

That will output the current auth_hash to logs/development.log. Use that to determine where in the hash the address data is. You'll then be able to do something like:

user.update address: auth_hash[:info][:address]

However, you may find that the address is not included in the data returned by the facebook oauth system. In which case you will need to return to their documentation to see if this is possible.

ReggieB
  • 8,100
  • 3
  • 38
  • 46