LoginSignup
8
8

More than 5 years have passed since last update.

OmniAuthのcallback先を動的に変更

Posted at
config/initializers/omniauth.rb

# encoding: utf-8

require 'omniauth-openid'
require 'openid/store/filesystem'

# callback先のドメインを現在のものに合わせる => reverse proxy用
OmniAuth.config.full_host = lambda do |env|
  # http or https
  scheme         = env['rack.url_scheme']

  # アプリケーションサーバーのアドレス
  local_host     = env['HTTP_HOST']

  # リバースプロキシサーバーのアドレス: リバースプロキシ側でヘッダーを送られないと意味がない
  forwarded_host = env['HTTP_X_FORWARDED_HOST']

  # リバースプロキシしてるかどうか
  forwarded_host.blank? ? "#{scheme}://#{local_host}" : "#{scheme}://#{forwarded_host}"
end

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, 'xxx', 'xxx'
  provider :facebook, 'xxx', 'xxx'

  # generic openid
  provider :open_id, :store => OpenID::Store::Filesystem.new('/tmp')
  provider :open_id, :name => 'google', :identifier => 'https://www.google.com/accounts/o8/id'
end

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