Help us understand the problem. What is going on with this article?

Rails上でOmniAuthを使う時のredirect_uriの設定

More than 5 years have passed since last update.

サーバの構成を変えてからなぜかOmniAuth認証ができなくなったので調べてみたら、

https://github.com/intridea/omniauth/blob/c3e3aa5bbfb589db1ebed40324c7eea2e3153f8c/lib/omniauth/strategy.rb

def full_host
  case OmniAuth.config.full_host
  when String
    OmniAuth.config.full_host
  when Proc
    OmniAuth.config.full_host.call(env)
  else
    # in Rack 1.3.x, request.url explodes if scheme is nil
    if request.scheme && request.url.match(URI::ABS_URI)
      uri = URI.parse(request.url.gsub(/\?.*$/, ''))
      uri.path = ''
      # sometimes the url is actually showing http inside rails because the
      # other layers (like nginx) have handled the ssl termination.
      uri.scheme = 'https' if ssl? # rubocop:disable BlockNesting
      uri.to_s
    else ''
    end
  end
end

def callback_url
  full_host + script_name + callback_path + query_string
end

ここのfull_hostが'http://backend' になってしまっているせいで、http://backend/auth/facebook/callback みたいなurlにリダイレクトされてしまうのが原因だった。

Railsのレイヤーで設定してしまった方がいいだろうということで、
config/environments/production.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  configure do |config|
    config.full_host = "https://サイトのurl"
  end

  #ここにOmniAuthの設定
end

としたらなおった。

yfuka86
railsとreactで書く人。
http://www.appbrew.io
appbrew-inc
「ユーザーが熱狂するプロダクトを再現性をもって創造する」をミッションにサービス開発・運営を行うスタートアップ
https://lipscosme.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away