如何处理多个环境中的OmniAuth回调? [英] How to handle OmniAuth callbacks in multiple environments?
问题描述
我有一个应用程序,专门用于提供身份验证的Facebook,并正确设置了生产模式的回调。为了使其工作,您可以为Facebook的应用提供一个网站URL和一个回调网站,在我的例子中,它是 http://appname.heroku.com
和 appname.heroku.com
。
I have an app that uses Facebook exclusively as a provider for authentication and have correctly setup the callback for production mode. In order for this to work, you provide your Facebook app with a site URL and a site domain for callbacks, and in my case it's http://appname.heroku.com
and appname.heroku.com
respectively.
问题是我的控制器设置为只允许经过身份验证的会话我无法在开发模式下查看我的应用程序,因为Facebook应用程序的域名显然尚未设置为localhost。
The problem is that my controllers are setup to only allow authenticated sessions so I cannot view my app in development mode because the Facebook app's domain obviously hasn't been set to localhost.
如何避免在Facebook的设置?
How do I get around this without having to change it in Facebook's settings?
推荐答案
创建另一个Facebook应用程序,域名为 localhost:3000
用于开发,并在 config
目录中创建
config.yml
Create another one facebook app with domain localhost:3000
for development and create
config.yml
in the config
directory
development:
facebook_api_key: 656756786867868
facebook_api_secret: 4sdffdh6u345436
production:
facebook_api_key: 45778799
facebook_api_secret: fghjkbbcbcbcbcb
现在将load_config.rb添加到initializers文件夹中
Now add load_config.rb to the initializers folder
# load config
AppConfig = YAML.load_file(Rails.root.join('config', 'config.yml'))
# Override config options by correct environment
env_options = AppConfig.delete(Rails.env)
AppConfig.merge!(env_options) unless env_options.nil?
最后添加到 omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, AppConfig['facebook_api_key'], AppConfig['facebook_api_secret']
end
在轨道环境。就这样,希望能帮助你。
It will take your keys depending on rails environment. That's all, hope it helps you.
这篇关于如何处理多个环境中的OmniAuth回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!