【问题标题】:have userid field in rails 3 using devise and omniauth使用设计和omniauth在rails 3中有用户标识字段
【发布时间】:2011-02-06 15:23:48
【问题描述】:

我有一个应用程序,它目前使用设计和omniauth 来使用gmail openID 进行身份验证。现在我想为每个用户分配一个唯一的用户 ID。所以他们仍然会使用他们的 gmail 凭据登录,但现在他们将拥有一个他们将被引用的用户 ID。

我怎样才能将它添加到我的架构中?

【问题讨论】:

    标签: ruby-on-rails-3 authentication devise omniauth


    【解决方案1】:

    当您创建用户表时,您的迁移脚本应该默认创建一个 id 列。此列中的值应该是唯一的,因此您可以将其用作所有用户的唯一 ID。如果这不能满足您的需求,您可以在数据库中添加另一列,然后在创建数据库中尚不存在的用户时将该字段设置为 OmniauthCallbacksController 中的唯一标识符。

    例如,在devise page 上的 facebook 示例中,您可以修改从 OmniauthCallbacksController 调用的 find_for_facebook_oauth 方法,以便在创建用户时创建新字段。

    def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
      data = access_token['extra']['user_hash']
      if user = User.find_by_email(data["email"])
        user
      else # Create an user with a stub password.
        # Add code here to create unique identifier for the user 
        # and make sure your field is passed to the create! method
        User.create!(:email => data["email"], :password => Devise.friendly_token[0,20]) 
      end
    end 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多