jeudi 5 janvier 2017

Rails Facebook Omniauth get user address

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

Aucun commentaire:

Enregistrer un commentaire