微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将OpenID存储移动到数据库而不是/ tmp

如何解决如何将OpenID存储移动到数据库而不是/ tmp

|| 我们有一个使用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方法取决于您的应用程序,并且它可能非常复杂。如果您需要更多帮助,请询问。 希望有帮助     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。