I have an application which currently uses devise and omniauth to authenticate using gmail openID. Now I wo开发者_运维技巧uld like to assign each user a unique user id. So they would still login using their gmail credentials but now they would have an user id they will be referenced to.
How can i go about adding that to my schema?
Your migration script should have created an id column by default when you created your user table. The values in this column should be unique so you could use that as a unique id for all users. If that doesn't meet your needs you could just add another column to your database and then have that field set to a unique identifier in your OmniauthCallbacksController when you are creating users that do not already exist in the database.
For example in the facebook example on the devise page you could modify the find_for_facebook_oauth method that is called from the OmniauthCallbacksController so that it creates your new field when a user is created.
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
精彩评论