Omniauth设计身份验证失败! csrf_detected:

时间:2015-06-27 02:46:38

标签: ruby-on-rails-4 devise omniauth

我得到了Authentication failure! csrf_detected:,因此我无法使用omniauth登录。

我遵循了tutorial

我找到了类似问题here

但直到现在我的问题都没有运气。

修复错误的想法?感谢

E, [2015-06-27T10:40:06.028200 #18798] ERROR -- omniauth: (facebook) Authentication failure! csrf_detected: OmniAuth::Strategies::OAuth2::CallbackError, csrf_detected | CSRF detected

宝石

  * devise (3.5.0)
  * omniauth (1.2.2)
  * omniauth-facebook (2.0.0)
  * omniauth-oauth2 (1.3.1)

/app/controllers/application_controller.rb

   protect_from_forgery with: :exception
-
+  before_action :authenticate_user!

/app/models/user.rb

   devise :database_authenticatable, :registerable,
+         :omniauthable, :omniauth_providers => [:facebook]
+  def self.from_omniauth(auth)
+    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
+      binding.pry
+      user.provider = auth.provider
+      user.uid = auth.uid
+      user.email = auth.info.email
+      user.password = Devise.friendly_token[0,20]
+    end
+  end
+

/config/initializers/omniauth.rb

+Rails.application.config.middleware.use OmniAuth::Builder do
+  provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
+   :scope => 'email'
+end

来自控制台的错误日志(如何跟踪它?)

Started GET "/users/auth/facebook/callback?code=AQDZC-Ny2PI-UwunCNi29mx4YGKT&state=cf896d3decffe2a7a664315e050a1165a290477542ff7d33" for 127.0.0.1 at 2015-06-27 10:40:05 +0800
I, [2015-06-27T10:40:05.255832 #18798]  INFO -- omniauth: (facebook) Callback phase initiated.
I, [2015-06-27T10:40:06.028051 #18798]  INFO -- omniauth: (facebook) Callback phase initiated.
E, [2015-06-27T10:40:06.028200 #18798] ERROR -- omniauth: (facebook) Authentication failure! csrf_detected: OmniAuth::Strategies::OAuth2::CallbackError, csrf_detected | CSRF detected
Processing by CallbacksController#failure as HTML
  Parameters: {"code"=>"AQDZC-Ny2PI-UwunCNi29mx4YGKTuHDeP2X2X-leywO14gr_iHLvXxX1LpV5WteUrQHpX-uc0Z01wcjy4XHA9CBkZeSo4qRb7jXdvPLfQl6mgwbMrFuQb1_55KughvtMWMlZ_7YEhtiLoEZH_2EvGXLbuKkUq", "state"=>"cf896d3decffe2a7a663"}

路由

+  devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }      

1 个答案:

答案 0 :(得分:0)

看起来您还在this question Github Repo上询问了Omniauth Facebook。它看起来似乎没有一个可靠的答案。

@dmcbrayer确实建议将初始化程序更改为:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'],
   :scope => 'email',
   :info_fields => 'email'
end

这是因为Facebook方面的API更改要求您明确要求info_fields

维护者@mkdynamic还希望您验证它是否已在较新版本(当时为3.0.0)中修复。