2011-06-24 37 views
2

我們有一個使用devise和omniauth並希望支持openID的rails應用程序。我們在單個服務器上工作,但它使用「/ tmp」作爲「文件系統存儲」。看起來這不適用於多應用程序服務器環境。如何將OpenID存儲移動到數據庫而不是/ tmp

我們如何創建一個數據庫存儲而不是標準的文件系統存儲?更好的是,有沒有一種寶石可以做到這一點?

感謝

回答

0

步驟如下:

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方法取決於您的應用程序,它可能非常複雜。如果您需要更多幫助,只需詢問。

希望這有助於

相關問題