Home > OS >  NoMethodError, undefined method `bytesize' for Hash with devise and omniauth-facebook
NoMethodError, undefined method `bytesize' for Hash with devise and omniauth-facebook

Time:02-22

I started to create a login with facebook followed this guide https://github.com/heartcombo/devise/wiki/OmniAuth:-Overview

I encountered this problem and this is my full logs start from go to login page until it failed

Started GET "/" for 183.81.126.179 at 2022-02-20 15:21:37  0700
Cannot render console from 183.81.126.179! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SessionsController#login as HTML
  Rendering sessions/login.html.erb within layouts/application
  Rendered sessions/login.html.erb within layouts/application (1.2ms)
Completed 200 OK in 15ms (Views: 13.7ms | ActiveRecord: 0.0ms)


Started POST "/users/auth/facebook" for 183.81.126.179 at 2022-02-20 15:21:44  0700
Cannot render console from 183.81.126.179! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
D, [2022-02-20T15:21:44.265312 #14707] DEBUG -- omniauth: (facebook) Request phase initiated.
Started GET "/users/auth/facebook/callback?code=AQAwLIvj8EHn8mvphCjq6WdhRXxwcbqewXpo0ybr9I_hd_cCXeeZ_FWLFtLaVKhkanZPs4E7Z1opl98GNnzdtS2KJxrr9H3rZi7xfxtwSy5RFCp13YuudaVVflsEClKe60O-j6ZJPNcQYprf5iFcGhBkPNa7aRQjtRv5uWS0L8qHmGTHxqusm47NgCW5n65POtXxD74FZ_QN2erdWXyBfoqb1OAi1rNTUF24kAtWUP5QvrUZab7LdP1YJ6DbnNpAT2WOSFfcdQJL_PdLPuDPsOEcVlDefEqBgiH-WFuXUxBlg5-TA5OMl043gSvvEHaLEr6MIqE9FoztzyhZoeDeBs1AfT57zqIt-Q28wc5KjuETqr_LxYs5nUKrCE9cGr2aPkM&state=2f9fcd6c4c29a1f712c75a4a48915d8f85a69bb7fbc4af45" for 183.81.126.179 at 2022-02-20 15:21:45  0700
Cannot render console from 183.81.126.179! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
D, [2022-02-20T15:21:45.766103 #14707] DEBUG -- omniauth: (facebook) Callback phase initiated.
E, [2022-02-20T15:21:45.837188 #14707] ERROR -- omniauth: (facebook) Authentication failure! undefined method `bytesize' for #<Hash:0x0000000115a33f70>: NoMethodError, undefined method `bytesize' for #<Hash:0x0000000115a33f70>
Processing by Users::OmniauthCallbacksController#failure as HTML
  Parameters: {"code"=>"AQAwLIvj8EHn8mvphCjq6WdhRXxwcbqewXpo0ybr9I_hd_cCXeeZ_FWLFtLaVKhkanZPs4E7Z1opl98GNnzdtS2KJxrr9H3rZi7xfxtwSy5RFCp13YuudaVVflsEClKe60O-j6ZJPNcQYprf5iFcGhBkPNa7aRQjtRv5uWS0L8qHmGTHxqusm47NgCW5n65POtXxD74FZ_QN2erdWXyBfoqb1OAi1rNTUF24kAtWUP5QvrUZab7LdP1YJ6DbnNpAT2WOSFfcdQJL_PdLPuDPsOEcVlDefEqBgiH-WFuXUxBlg5-TA5OMl043gSvvEHaLEr6MIqE9FoztzyhZoeDeBs1AfT57zqIt-Q28wc5KjuETqr_LxYs5nUKrCE9cGr2aPkM", "state"=>"2f9fcd6c4c29a1f712c75a4a48915d8f85a69bb7fbc4af45"}
Redirected to https://f8a1-183-81-126-179.ngrok.io/
Completed 302 Found in 0ms (ActiveRecord: 0.0ms)


Started GET "/" for 183.81.126.179 at 2022-02-20 15:21:46  0700
Cannot render console from 183.81.126.179! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SessionsController#login as HTML
  Rendering sessions/login.html.erb within layouts/application
  Rendered sessions/login.html.erb within layouts/application (0.8ms)
Completed 200 OK in 15ms (Views: 13.7ms | ActiveRecord: 0.0ms)

I tried to debug in the users/omniauth_callbacks_controller, but it only break in the failure action

From my understanding,

I clicked the login button -> Send POST request to Facebook App -> Facebook App will callback to the (users/auth/facebook/callback). Then, the facebook action will be executed But, it did not work and redirect to failure.

So, I guess that the problem before I go to the action. But I don't know why, and how. I tried to research in the Internet but no one encounters the same problem like me.

Please help, this problem pisses me off.....huhuhu


UPDATE

I checked the params in the failure action and reproduced the problem

Started POST "/users/auth/facebook" for 183.81.126.179 at 2022-02-20 15:44:36  0700
Cannot render console from 183.81.126.179! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
D, [2022-02-20T15:44:36.655205 #16338] DEBUG -- omniauth: (facebook) Request phase initiated.
Started GET "/users/auth/facebook/callback?code=AQALKVl94pGq_EVYU-CY9dSkXcGXEUGa-6o5InAiiyhoBNprBHgiWwkcFwLlT0Ozs1xvoxDXyeyvflm9iow_forJdv3Gg2Orq_Bw6zDblbHmfLbDPZW2tx-Ap-amZf29Ao-IiEQRx-SO7hOBAr1NdFmyU8U_SGeMzdEweumGvjR0bE8D8-sLuyk6PVsxcEuHGOI6GExT2HVNQnVF7xajfRifJzusxuuzakaB2KJbhj40bi7lfgXQPnXS2ZhWY2ZdvTCvGSq6uadhd3wC9olfetEIS_Odfx1MCSxG6I6h0iG31dhRthLDMt6MOSZknjJrMzJ99KQErhOzGQUx2-nAF60J8Kq5mCzriqYNw5yqjAndmVznCmmMakPIpghGH6l9Ym8&state=5ef298ab4cce896a4f11a29c1c18e58011f603cbdc4fabeb" for 183.81.126.179 at 2022-02-20 15:44:37  0700
Cannot render console from 183.81.126.179! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
D, [2022-02-20T15:44:37.967905 #16338] DEBUG -- omniauth: (facebook) Callback phase initiated.
E, [2022-02-20T15:44:38.039349 #16338] ERROR -- omniauth: (facebook) Authentication failure! undefined method `bytesize' for #<Hash:0x00000001295b0d68>: NoMethodError, undefined method `bytesize' for #<Hash:0x00000001295b0d68>
Processing by Users::OmniauthCallbacksController#failure as HTML
  Parameters: {"code"=>"AQALKVl94pGq_EVYU-CY9dSkXcGXEUGa-6o5InAiiyhoBNprBHgiWwkcFwLlT0Ozs1xvoxDXyeyvflm9iow_forJdv3Gg2Orq_Bw6zDblbHmfLbDPZW2tx-Ap-amZf29Ao-IiEQRx-SO7hOBAr1NdFmyU8U_SGeMzdEweumGvjR0bE8D8-sLuyk6PVsxcEuHGOI6GExT2HVNQnVF7xajfRifJzusxuuzakaB2KJbhj40bi7lfgXQPnXS2ZhWY2ZdvTCvGSq6uadhd3wC9olfetEIS_Odfx1MCSxG6I6h0iG31dhRthLDMt6MOSZknjJrMzJ99KQErhOzGQUx2-nAF60J8Kq5mCzriqYNw5yqjAndmVznCmmMakPIpghGH6l9Ym8", "state"=>"5ef298ab4cce896a4f11a29c1c18e58011f603cbdc4fabeb"}

From: /Users/jackie/Desktop/fbbm/app/controllers/users/omniauth_callbacks_controller.rb:20 Users::OmniauthCallbacksController#failure:

    19: def failure
 => 20:   binding.pry
    21:   redirect_to root_path
    22: end

[1] pry(#<Users::OmniauthCallbacksController>)> request.env["omniauth.auth"]
=> nil
[2] pry(#<Users::OmniauthCallbacksController>)> params
=> <ActionController::Parameters {"code"=>"AQALKVl94pGq_EVYU-CY9dSkXcGXEUGa-6o5InAiiyhoBNprBHgiWwkcFwLlT0Ozs1xvoxDXyeyvflm9iow_forJdv3Gg2Orq_Bw6zDblbHmfLbDPZW2tx-Ap-amZf29Ao-IiEQRx-SO7hOBAr1NdFmyU8U_SGeMzdEweumGvjR0bE8D8-sLuyk6PVsxcEuHGOI6GExT2HVNQnVF7xajfRifJzusxuuzakaB2KJbhj40bi7lfgXQPnXS2ZhWY2ZdvTCvGSq6uadhd3wC9olfetEIS_Odfx1MCSxG6I6h0iG31dhRthLDMt6MOSZknjJrMzJ99KQErhOzGQUx2-nAF60J8Kq5mCzriqYNw5yqjAndmVznCmmMakPIpghGH6l9Ym8", "state"=>"5ef298ab4cce896a4f11a29c1c18e58011f603cbdc4fabeb"} permitted: false>
[3] pry(#<Users::OmniauthCallbacksController>)> params.to_string
NoMethodError: undefined method `to_string' for #<ActionController::Parameters:0x00000001191a3c80>
from (pry):3:in `failure'
Caused by NoMethodError: undefined method `bytesize' for #<Hash:0x00000001295b0d68>
from /Users/jackie/.rbenv/versions/2.7.2/lib/ruby/2.7.0/net/http/generic_request.rb:184:in `send_request_with_body'
[4] pry(#<Users::OmniauthCallbacksController>)> Started GET "/" for 181.214.68.57 at 2022-02-20 15:46:15  0700
Cannot render console from 181.214.68.57! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SessionsController#login as HTML
  Rendering sessions/login.html.erb within layouts/application
  Rendered sessions/login.html.erb within layouts/application (0.9ms)
Completed 200 OK in 14ms (Views: 12.6ms | ActiveRecord: 0.0ms)

Perhaps the params can not to_string but I don't know where call this func

CodePudding user response:

I've been having similar issues for ages with the github omniauth strategy.

Not sure what your Gemfile looks like but when I added this line instead and ran bundle install again everything started working as expected for me.

gem 'omniauth-github', github: 'omniauth/omniauth-github', branch: 'master'

In your case I think the equivalent would be:

gem 'omniauth-facebook', github: 'simi/omniauth-github', branch: 'master'

CodePudding user response:

I used this in my Gemfile

gem "omniauth-rails_csrf_protection"
gem 'omniauth-github', github: 'omniauth/omniauth-github', branch: 'master'
gem 'omniauth-facebook', github: 'simi/omniauth-facebook', branch: 'master'
  • Related