Omniauth-Facebook回调在Rails可安装引擎中未初始化 [英] Omniauth-Facebook callback does not get initialised in Rails Mountable Engine
问题描述
我正在开发一个可安装的引擎(称为SimpleUser),它使用Devise,OmniAuth和OmniAuth-Facebook。首先,我做了一个测试应用程序与宝石关于和一切工作正常。接下来,我从头开始构建引擎,使用测试应用程序的代码作为示例。
I'm developing a mountable engine (called SimpleUser) which uses Devise, OmniAuth and OmniAuth-Facebook. First I made a test app with the gems about and every worked fine. Next, I started building the engine from scratch, using the code of the test app as an example.
除了与Facebook的连接之外,所有内容都已完成Javascript弹出窗口)。当我点击登录时,显示FB弹出窗口,我授予应用程序,它重定向到指定的路由(参见路线),但会抛出此错误:
Everything is almost done, except for the connection with Facebook (it uses the Javascript popup). When I click in "log in" the FB popup is displayed, I grant the app, it redirects to the route specified (see routes), but throws this error:
NoMethodError in SimpleUser::AuthController#create
undefined method `[]' for nil:NilClass
该行为出现错误,在 authentication = Authentication.find_by_provider_and_uid(auth ['provider'],auth ['uid'])
其中 auth 是nil( auth 来自 auth = request.env [omniauth.auth]
)
The error occurs in that action, in the line authentication = Authentication.find_by_provider_and_uid(auth['provider'], auth['uid'])
where auth is nil (auth comes from auth = request.env["omniauth.auth"]
).
我检查的一件事是回调阶段没有初始化。这是测试应用程序的日志:
One thing I check is that the Callback phase it's no initialised. This is the log of the test app:
在2013-03-03开始获取127.0.0.1的/ auth / facebook / callback 14
08:52:56 -0600(facebook)回调阶段启动。处理
AuthController#create as HTML参数:{provider=>facebook}
Started GET "/auth/facebook/callback" for 127.0.0.1 at 2013-03-14 08:52:56 -0600 (facebook) Callback phase initiated. Processing by AuthController#create as HTML Parameters: {"provider"=>"facebook"}
这是日志的引擎应用程序:
This is the log of the engine app:
Started GET "/simple_user/auth/facebook/callback" for 127.0.0.1 at 2013-03-14 08:51:19 -0600
Processing by SimpleUser::AuthController#create as HTML
Parameters: {"provider"=>"facebook"}
为了管理OmniAuth,我将gem添加到.gemspec和Gemfile;而且,我需要引擎中的宝石,而在引擎的发生器内,我会在安装过程中将omniauth.rb的模板移动到父应用的配置/初始化器。这是我所拥有的:
For manage OmniAuth, I added the gem to the .gemspec and to the Gemfile; also, I require the gems in the engine, and within a generator of the engine I move a template of omniauth.rb to config/initializers of the parent app during installation. This is what I have:
AuthController(位于app / controllers / simple_user / auth_controller.rb)
module SimpleUser
class AuthController < ApplicationController
def create
auth = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(auth['provider'], auth['uid'])
if authentication
flash[:notice] = "Signed in successfully."
sign_in(:user, authentication.user)
redirect_to root_url
else
user = User.build_new_auth(auth)
#user.apply_omniauth(auth)
if user.save(:validate => false)
flash[:notice] = "Account created and signed in successfully."
sign_in(:user, user)
redirect_to root_url
else
flash[:error] = "Error while creating the user account. Please try again."
redirect_to root_url
end
end
end
end
end
引擎
module SimpleUser
require 'rubygems'
require 'devise'
require 'cancan'
require 'rolify'
require 'omniauth'
require 'omniauth-facebook'
require 'simple_form'
class Engine < ::Rails::Engine
isolate_namespace SimpleUser
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'fb_config.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value.to_s
end if File.exists?(env_file)
env_file = File.join(Rails.root, 'config', 'devise_config.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value.to_s
end if File.exists?(env_file)
end
end
end
生成器
module SimpleUser
module Generators
class InstallGenerator < ::Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)
desc "Install SimpleUser"
def copy_config_file
copy_file "fb_config.yml", "config/fb_config.yml"
copy_file "devise_config.yml", "config/devise_config.yml"
copy_file "omniauth.rb", "config/initializers/omniauth.rb"
end
def copy_migrations
rake "simple_user:install:migrations"
SimpleUser::Engine.load_seed
end
end
end
end
omniauth的模板。 rb
require 'omniauth'
require 'omniauth-facebook'
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], :scope => ENV['FACEBOOK_SCOPE']
end
路线(引擎) strong>
Routes (on engine)
match 'auth/:provider/callback', to: 'auth#create'
match 'auth/failure', to: redirect('/')
路由(在虚拟应用程序)
mount SimpleUser::Engine => "/simple_user", :as => "simple_user"
.gemspec依赖关系
s.add_dependency "rails", "~> 3.2.12"
s.add_dependency "devise"
s.add_dependency "cancan"
s.add_dependency "rolify"
s.add_dependency "omniauth"
s.add_dependency "omniauth-facebook", "1.4.1"
s.add_dependency "simple_form"
#s.add_development_dependency "mysql2"
s.add_development_dependency "sqlite3"
s.add_development_dependency "jquery-rails"
s.add_development_dependency "debugger"
Gemfile
source "http://rubygems.org"
gemspec
gem 'devise'
gem 'cancan'
gem 'rolify'
gem 'omniauth'
gem 'omniauth-facebook', '1.4.1'
gem 'simple_form'
# Development
gem 'jquery-rails'
gem 'debugger'
我认为问题是没有初始化的回调,原因可能是OmniAuth没有加载,但我不知道是否和如何解决。
I think the problem is the callback that is not initialised, and the reason may be that OmniAuth doesn't get loaded, but I don't know if it is and how to solve it.
您可以在 https://github.com/pablomarti/simple_user 中查看项目,如果你想克隆它,并测试你可以使用生成器 rails g simple_user:install
,你可以看到test / dummy的代码也可以得到这个想法。
You can check the project in https://github.com/pablomarti/simple_user, and if you want to clone it and test you can use the generator rails g simple_user:install
, and you can see the code of test/dummy also to get the idea.
非常感谢您提前。
推荐答案
解决方案是要删除omniauth.rb并在引擎中包含OmniAuth的中间件,所以引擎看起来像这样:
The solution was to remove the omniauth.rb and include the middleware of OmniAuth in the engine, so the engine looks like this:
module SimpleUser
require 'rubygems'
require 'devise'
require 'cancan'
require 'rolify'
require 'omniauth'
require 'omniauth-facebook'
require 'simple_form'
class Engine < ::Rails::Engine
isolate_namespace SimpleUser
middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], :scope => ENV['FACEBOOK_SCOPE']
end
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'fb_config.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value.to_s
end if File.exists?(env_file)
env_file = File.join(Rails.root, 'config', 'devise_config.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value.to_s
end if File.exists?(env_file)
end
end
end
> https://stackoverflow.com/a/8413724/347501 在类似的问题。
这篇关于Omniauth-Facebook回调在Rails可安装引擎中未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!