LoginSignup
4
3

More than 3 years have passed since last update.

localhostで(google_oauth2) Authentication failure! csrf_detected: OmniAuth::Strategies::OAuth2::CallbackError, csrf_detected | CSRF detectedって言われた時の対応

Posted at

結論

今回はlocalhostで試していたので、config/devise/session_store.rbを下記のように変更すればOK。

if Rails.env.production?
  Rails.application.config.session_store :cookie_store, key: '_my_session', expire_after: 1.weeks, domain: 'mydomain.com'
else
  Rails.application.config.session_store :cookie_store, key: '_my_session', domain: 'localhost'
end

原因など

Ruby On Railsのdeviseは本当に優秀であっという間にFacebookのログインは出来るようになったのですが、どうもlocalhostで試した時に「Google OAuthの挙動がおかしいような?」となり調べました。

どうもエラーでググると「provider_ignores_state: true を設定して解決!」みたいな記事がちらほら見つかりましたが、普通に考えてそれって必要だからチェックをしてエラーを吐いているわけですよね?

なので、もう少し調べてみると、ドンピシャの記事を発見!

ドンピシャの記事によると、「/config/initializer/session_store.rb」に定義されているドメインとGoogle APIの「origin/redirect_uri」に定義されているドメインが違うと発生するとのこと。

というわけで、冒頭の対応方法で解決できました。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3