Reputation: 13927
I have a Rails app using the Omni-Auth GitHub gem. User account creation and signin via GitHub works flawlessly!
Here's my User model:
def self.from_omniauth(auth)
find_by_github_uid(auth["uid"]) || create_from_omniauth(auth)
end
def self.create_from_omniauth(auth)
create! do |user|
user.github_uid = auth["uid"]
user.github_token = auth["credentials"]["token"]
user.username = auth["info"]["nickname"]
user.email = auth["info"]["email"]
user.full_name = auth["info"]["name"]
user.gravatar_id = auth["extra"]["raw_info"]["gravatar_id"]
user.blog_url = auth["extra"]["raw_info"]["blog"]
user.company = auth["extra"]["raw_info"]["company"]
user.location = auth["extra"]["raw_info"]["location"]
user.hireable = auth["extra"]["raw_info"]["hireable"]
user.bio = auth["extra"]["raw_info"]["bio"]
end
end
But sometimes users change their bio or company or wether or not they want to be hired, so I rather than having to delete old accounts, I figured it would be nice if it updated for them in a timely fashion after they updated their account.
What's the best practice for doing this and how can I use my existing OmniAuth code to update the user's information?
Upvotes: 0
Views: 487
Reputation: 1675
def self.from_omniauth(auth)
user = find_by_github_uid(auth["uid"]) || User.new
user.assign_from_omniauth(auth).save!
end
def assign_from_omniauth(auth)
self.github_uid ||= auth["uid"]
self.github_token ||= auth["credentials"]["token"]
self.username ||= auth["info"]["nickname"]
self.email ||= auth["info"]["email"]
self.full_name = auth["info"]["name"]
self.gravatar_id = auth["extra"]["raw_info"]["gravatar_id"]
self.blog_url = auth["extra"]["raw_info"]["blog"]
self.company = auth["extra"]["raw_info"]["company"]
self.location = auth["extra"]["raw_info"]["location"]
self.hireable = auth["extra"]["raw_info"]["hireable"]
self.bio = auth["extra"]["raw_info"]["bio"]
end
Upvotes: 5