问题描述
||
我们有一个使用devise和omniauth的Rails应用程序,并希望支持openID。我们在单个服务器上运行它,但是它的\“文件系统存储\”使用\“ / tmp \”。看来这不适用于多应用程序服务器环境。
我们如何创建数据库存储而不是标准文件系统存储?更好的是,有宝石可以做到吗?
谢谢
解决方法
现在实际上有一个瑰宝:
https://github.com/TheNeatCompany/openid-activerecord-store
# Omniauth example:
Rails.application.config.middleware.use(
OmniAuth::Strategies::GoogleApps,OpenID::Store::ActiveRecord.new,{ :name => \'example\',:domain => \'example.org\' }
)
, 步骤如下:
1)创建一个表来存储身份验证令牌:让我们将其命名为身份验证
class CreateAuthentications < ActiveRecord::Migration
def self.up
create_table :authentications do |t|
t.integer :user_id
t.string :provider
t.string :uid
t.string :provider_name
t.string :provider_username
t.text :token
t.timestamps
end
add_index :authentications,:user_id
add_index :authentications,:provider
add_index :authentications,:uid
add_index :authentications,:token,:unique=>true
end
def self.down
drop_table :authentications
end
end
2)修改OmniauthCallbacksController :: process_callback方法(这是我用来处理来自OpenId和Facebook服务的所有回调的方法)以创建身份验证记录并将其存储在新创建的身份验证表中:
def process_callbacks
# ...some code here
# Find if an authentication token for this provider and user id already exists
authentication = Authentication.find_by_provider_and_uid(@omniauth_hash[\'provider\'],@omniauth_hash[\'uid\'])
if authentication # We found an authentication
if user_signed_in? && (authentication.user.id != current_user.id)
flash[:error] = I18n.t \"controllers.omniauth_callbacks.process_callback.error.account_already_taken\",:provider => registration_hash[:provider].capitalize,:account => registration_hash[:email]
redirect_to edit_user_account_path(current_user)
return
end
else
# We could not find the authentication than create one
authentication = Authentication.new(:provider => @omniauth_hash[\'provider\'],:uid => @omniauth_hash[\'uid\'])
if user_signed_in?
authentication.user = current_user
else
registration_hash[:skip_confirmation] = true
authentication.user = User.find_by_email(registration_hash[:email]) || User.create_user(registration_hash)
end
end
@user = authentication.user
# save the authentication
authentication.token = @omniauth_hash
authentication.provider_name = registration_hash[:provider]
authentication.provider_username = registration_hash[:email]
if !authentication.save
logger.error(authentication.errors)
end
#...more code here
end
3)既然做完了,去喝杯好咖啡。
现在请注意,process_callbacks方法取决于您的应用程序,并且它可能非常复杂。如果您需要更多帮助,请询问。
希望有帮助