我正在開發一個使用Devise,OmniAuth和OmniAuth-Facebook的可掛載引擎(稱爲SimpleUser)。首先,我製作了一個測試應用程序,其中包含關於和每一個工作正常的寶石接下來,我開始從頭開始構建引擎,以測試應用程序的代碼爲例。Omniauth-Facebook回調不會在Rails可掛載引擎中初始化
除了與Facebook的連接(它使用Javascript彈出窗口),一切都快完成了。當我點擊「登錄」顯示FB彈出,我授予應用程序,它重定向到指定的(見路線)的路線,但引發此錯誤:
NoMethodError in SimpleUser::AuthController#create
undefined method `[]' for nil:NilClass
在行動出現的錯誤,在該行authentication = Authentication.find_by_provider_and_uid(auth['provider'], auth['uid'])
其中驗證碼爲零(驗證碼來自auth = request.env["omniauth.auth"]
)。
我檢查的一件事是回調階段它沒有初始化。這是測試應用程序的日誌:
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"}
這是發動機的應用程序的日誌:
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,我添加了寶石的.gemspec和到Gemfile文件;另外,我需要引擎中的寶石,並且在引擎的生成器中,我將omniauth.rb的模板在安裝過程中移動到父應用的配置/初始化器。這是我有:
AuthController(位於應用程序/控制器/ 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
路線(發動機)
match 'auth/:provider/callback', to: 'auth#create'
match 'auth/failure', to: redirect('/')
路線(上虛設應用)
mount SimpleUser::Engine => "/simple_user", :as => "simple_user"
.gem的規範依賴
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不會被加載,但我不知道它是否以及如何解決它。
你可以在https://github.com/pablomarti/simple_user中檢查項目,如果你想克隆它並測試你可以使用生成器rails g simple_user:install
,你也可以看到test/dummy的代碼來獲得這個想法。
非常感謝您提前。