LoginSignup
10
7

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-10-21

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

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

としたらなおった。

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