1

I got some problem with OmniAuth FaceBook login into my webapp. Tell me please, what should i edit to fix this error? Check the code.

Error in the browser:

undefined method `to_a' for "Name Surname":String

User.rb:

def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      x = auth.info.name.to_a
      user.name = x[0]
      user.surname = x[1]
      user.login = auth.info.uid
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]

    end
  end

Omniauth_callback_controller.rb :

def facebook
    if request.env["omniauth.auth"].info.email.blank?
      redirect_to "/users/auth/facebook?auth_type=rerequest&scope=email"
    end
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      if(@user.surname.nil?)
        redirect_to new_profile_path
      else
        redirect_to my_profile_path
      end
    end
  end

devise.rb :

config.omniauth :facebook, '*********', '*********', {:client_options => {:ssl => {:verify => false}}}
  • Facebook auth can provide `auth.info.first_name` and `auth.info.last_name` separately, it will be more reliable in case users have more than two names to access them this way. See this other SO [question](http://stackoverflow.com/questions/33090322/get-first-name-and-last-name-fields-from-facebook-omniauth). – Sinan Guclu Jul 08 '16 at 12:47

3 Answers3

0

name is a string and you are trying to convert it to array

x = auth.info.name.to_a

use split instead

x = auth.info.name.split
#=> ['Name', 'Surname']
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
0

You can make use of OmniAuth first_name and last_name:

Update your devise.rb:

config.omniauth :facebook, '*********', '*********', info_fields: 'email, first_name, last_name', {:client_options => {:ssl => {:verify => false}}}

and your user.rb:

def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|

      user.name = auth.info.first_name
      user.surname = auth.info.last_name
      user.login = auth.info.uid
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]

    end
  end
Sinan Guclu
  • 1,075
  • 6
  • 16
  • When i pasted 'info_fields: 'email, first_name, last_name' , RubyMine yell me about syntax error and underline this part of code – Vladyslav Kalyuzhnyy Jul 08 '16 at 13:09
  • It is correct as per the omniauth documentation: `config.omniauth :facebook, "APP_ID", "APP_SECRET", scope: 'email', info_fields: 'email,name' ` as shown [here](https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview). Have you tried to run it? – Sinan Guclu Jul 08 '16 at 13:13
  • Now, when i hit Login with facebook...it redirect me back to my localhost but now url look like this : http://localhost:3000/#_=_ – Vladyslav Kalyuzhnyy Jul 08 '16 at 13:20
  • So it takes you back to a non auth protected page? Can you share your User controller? The #_-_ character is added by Facebook to the callback URL. Do you have the omni_ath callbacks set correctly in your routes.rb? ie: `devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }` – Sinan Guclu Jul 08 '16 at 13:34
  • Yes i have omniauth in routes.rb – Vladyslav Kalyuzhnyy Jul 09 '16 at 10:26
0

your user.rb should be like this

def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do  |user|
      x = auth.info.name.split
      if x.count > 1
        user.name = x[0]
        user.surname = x[1]
      else
        user.name = x[0]
      end 
      user.login = auth.info.uid
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
    end
end

But I would suggest to store in a single field, because name can be of more than two words so it would become difficult to identify name and surname.

this link should also help

power
  • 1,225
  • 7
  • 16