Home > Net >  Rails 6 omniauth working on local but not production (Heroku)
Rails 6 omniauth working on local but not production (Heroku)

Time:02-20

I have a:

  • -rails 6 app
  • -deploy on Heroku
  • -Devise with omniauth both with Facebook and google
  • -On locale omniauth is working as expected
  • -On stagging and production (heroku) i am getting the following error once i select the google (or facebook) with which i want to connect and get redirected to home page without any other action:

Authentication failure! undefined method `bytesize' for #<Hash:...

Here is what my links look like:

 <%= link_to t('navbar.loginf'), user_facebook_omniauth_authorize_path,  method: :post, class: "is-fullwidth" %>

<%= link_to t('navbar.loging'), user_google_oauth2_omniauth_authorize_path, method: :post, class: "is-fullwidth" %>

This is omniauth controller:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    @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']
      redirect_to new_user_registration_url
    end
  end

  def google_oauth2
    @user = User.from_omniauth(request.env['omniauth.auth'])
    if @user.persisted?
      sign_in_and_redirect @user
      set_flash_message(:notice, :success, kind: 'Google') if is_navigational_format?
    else
      flash[:error] = I18n.t('alert.gge')
      redirect_to new_user_registration_url
    end
  end

  def failure
    redirect_to root_path
  end
end

CodePudding user response:

Find the issue:

-Had to update yarn.lock file (delete the yarn.lock file And run the yarn install commad on terminal it will generate a new yarn.lock )

-This version was different on heroku

"@babel/parser@^7.16.7", "@babel/parser@^7.17.3":

-Google callback call the JS file to hit the action with the params that we are requested.

-So Omniauth need to parse it but its not able to do this because the babel is not parsing it properly and give it to auth_hash method of omniauth.rb

CodePudding user response:

I have a similar problem that I follow this guide https://github.com/heartcombo/devise/wiki/OmniAuth:-Overview

When I clicked the sign in button -> the system will send a post request to Facebook App, then the facebook app will callback to the Rails app. At that moment, the log will be showed and redirected to the failure function.

This is my problem.

I believed that this is a problem from devise or omniauth, but I don't know why.

What should I do now? I'm using ruby 2.7.2 and rails 5.2.6

  • Related