Devise/Omniauth failure: How to debug it?

♀尐吖头ヾ 提交于 2019-12-05 00:07:15

To answer the original question about how you debug Omniauth, here's how to enable logging for Omniauth. Add this line to config/initializers/devise.rb just after you define your Omniauth strategies:

OmniAuth.config.logger = Rails.logger if Rails.env.development?

(If you're not using Devise, just Omniauth, then add the code instead to config/initializers/omniauth.rb)

You'll get loads more information from Omniauth in your log file - including the full response from the callback phase.

I know this is an old question, but I had this same issue and nothing I found on the net helped. Turns out the issue was caused by how I was using Puma (practices brought over from Thin). I was starting multiple Puma processes on the same machine (Apache reverse proxy) and it appears the call-back from Github was going to a different Puma process than the original authentication request. I took the Puma processes down to one and didn't have the condition again (switched to using Puma workers: puma -w 5, which in turn uses processes that are managed by Puma instead of Apache round robin). This is also why I never stumbled on the issue in development because I don't run a cluster of processes in that environment.

I realize that this as an old question and that the person who originally asked it either solved it or moved to some other solution, however, I encountered the same problem, and my quest for solution led me here.

I managed to solve it, so here it is for the next person looking for it.

It turned out to be trivial. This is what I had:

# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :github, ENV['github_key'], ENV['github_secret']
end

# config/initializers/devise.rb
Devise.setup do |config|
  # ... devise config not related to omniauth ...
  config.omniauth :github, ENV['github_key'], ENV['github_secret']
end

So, I was configuring the omniauth provider in two places, and it was conflicting.

After I removed the config from omniauth.rb, the redirection request from github to /auth/github/callback started being processed by Users::OmniauthCallbacksController#github instead of #failure as was the situation prior to the change.

So dumb error and so little info to work with...

Did you recently upgrade your gems? If so, it might be useful to compare omniauth and devise versions with the previous gem versions. It could also be an omniauth/devise dependency that changed.

I am not exactly sure what the problem is in this specific case, but if you want to dig deep in the code, then install the pry-debug gem. It gives you the pry interface with step and next debug commands. Add pry.binding in your code and it will stop execution in the server and bring up the pry interface. For example:

def failure
  binding.pry
  render :text => params.inspect
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!