2011-02-06 105 views
0

我有一個應用程序,它目前使用devise和omniauth使用gmail openID進行身份驗證。現在我想爲每個用戶分配一個唯一的用戶ID。所以他們仍然會使用他們的gmail憑證登錄,但現在他們將擁有一個用戶ID,他們將被引用。在rails 3中使用userid字段使用devise和omniauth

我該如何去添加到我的模式?

回答

0

當您創建用戶表時,您的遷移腳本應該默認創建了一個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 
相關問題