0
0

More than 1 year has passed since last update.

deviseをアップデートしたらomniauthエラーで詰まった

Last updated at Posted at 2023-01-17

はじめに

deviseをアップグレードするために、omniauthのバージョンも同時にアップグレードしたら一部のコールバックがnilになってハマってしまったので共有します。

やったこと

  • device, omniauthをアップグレードした
Gemfile
- gem 'devise', '4.4.3'
- gem 'omniauth', '~> 1.9.0'

+ gem 'devise'
+ gem 'omniauth'
Gemfile.lock
- devise (4.4.3)
+ devise (4.8.1)

- omniauth (1.9.2)
+ omniauth (2.1.0)

発生した問題

oauth認証で必要な値がnilになってしまった。

[1] pry> request.env['omniauth.auth']
=> nil

試した対応方法

@jnitoさんからコメントをいただいた以下の箇所についても試してみたが変わらず。

上の記事にも書かれているとおり、omniauth-rails_csrf_protection gemを追加し、認可画面URLへのリダイレクトさせるリンクへのアクセス方法をGETからPOSTに変更してみてください。

こちらのコメントに書かれているcallback_pathを明示的に追加するやりかたも試しましたが、変わらず。

devise.rb
config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], scope: 'email,public_profile', locale: 'ja_JP', callback_path: "/users/auth/facebook/callback"

解決方法

  • こちらにも同様の現象が起きているとissueがあがっていたのを発見。

  • omniauthが2系以上にならないように対応。
Gemfile
- gem 'omniauth'
+ gem 'omniauth', '~> 1.9.1'
  • 無事に必要な値が取得できました!
    ※今回はどうやっても修正不可能だったのでgemのバージョンを2系未満で固定していますが、脆弱性の観点から最新のgemにバージョンアップするのが理想です。
Gemfile
[6] pry> request.env['omniauth.auth']
=> {"provider"=>"slack",
 "uid"=>"xxxxxxxxxxxx",
 "info"=>
  {"name"=>nil,
   "ok"=>true,
   "app_id"=>"xxxxxxxxxxx",
   "authed_user"=>{"id"=>"xxxxxxxxxxxx"},
   "scope"=>"channels:read,groups:read,incoming-webhook",
   "token_type"=>"bot",
    ...
    ...
0
0
2

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
0
0