LoginSignup
1
0

omniauth_openid_connectのリダイレクト先でrequest.env['omniauth.auth']がnilの解決法

Posted at

あれ

railsでomniauthとomniauth_openid_connectでCognitoの認証するぞ〜ってなってリダイレクト後の処理がはまったあれ

結論

なぜかrequest.env['omniauth.auth']がnilになってしまたあなたへ
たぶんコールバックのURLが auth/:provider/callback ではない場合にこうなる模様

解決法として callback_path を追加する

config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :openid_connect, {
    issuer: "https://cognito-idp.リージョン.amazonaws.com/プールID",
    discovery: true,
    scope: [:openid],
    response_type: :code,
+   callback_path: "/my/custom/callback", # ここにcallback_pathを追加!!
    client_options: {
      identifier: "***",
      secret: "***",
      redirect_uri: "http://exapmle.com/my/custom/callback",
    }
  }
end

controllers/session_controller.rb
class SessionsController < ActionController::Base
  def create

    # きっとするとnilじゃなくなる
    auth_hash = request.env['omniauth.auth']
  end
end
config/routes.rb
# やんごとなき理由で auth/:provider/callbackじゃないケース
get 'my/:provider/callback' => 'sessions#create'

たぶん

auth/:provider/callback の形式のURLをコールバックにしとくのが無難

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