Problems with login omniauth-facebook ruby on rails

0

I am trying to make a connection with facebook and ruby on rails using the omniauth-facebook, but when I try to make a connection it sends me an error of "no implicit conversion of Symbol into Integer" , would be very helpful if you can tell me where I am making the mistake this is the code I am using:

in the initializers folder I created an omniauth.rb file:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'xxxxface_book_idxxxxx', 'xxxsecret_id_facebookxxxxxx'
end

I create a route in routes.rb:

  get 'auth/:provider/callback', to: 'sessions#create'
  get 'auth/failure', to: redirect('/')
  get 'signout', to: 'sessions#destroy', as: 'signout'

followed by a controller sessions_controller.rb:

  def create
    user = User.from_omniauth("omniauth.auth")
    session[:user_id] = user.id
    redirect_to root_url
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url
  end

Then I create a model called user.rb:

  def self.from_omniauth(auth)
    where(auth.slice(:provider,:uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end

Then in application_controller.rb:

private

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  helper_method :current_user

Then I add in application.html.erb:

<% if current_user%>
  Signed in as <strong><%= current_user.name %></strong>
  <%= link_to "Sign out", signout_path, id: "sign_out"%>
<% else %>
  <%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in"%>
<% end %>

but when I click on the link it sends me the error:

Any ideas where I may be making the mistake ??? I will really appreciate if you can help me with this.

    
asked by JULIO MACHAN 17.08.2017 в 15:48
source

2 answers

1

I have not used facebook-omniauth before, but seeing the documentation I think your mistake is going to use :

User.from_omniauth("omniauth.auth")

instead of:

User.from_omniauth(request.env["omniauth.auth"])

then then calling auth.slice(:provider,:uid) you're calling the .slice method of String that does not allow symbols instead of of Hash that returns request.env if you allow them.

    
answered by 17.08.2017 в 16:17
0
  

send me this:

     

ActiveModel::ForbiddenAttributesError

     

marking this line:

     

where(auth.slice(:provider,:uid)).first_or_initialize.tap do |user|

You need to modify the code so that rails can accept the attributes provider and uid correctly.

To fix it, change the line that shows the error by this one:

where(provider: auth.provider, uid: auth.uid).first_or_initialize do |user|
    
answered by 17.08.2017 в 23:00