Skip to content

Instantly share code, notes, and snippets.

@benpolinsky
Last active August 29, 2015 14:18
Show Gist options
  • Save benpolinsky/67e776a5ab9a7062b777 to your computer and use it in GitHub Desktop.
Save benpolinsky/67e776a5ab9a7062b777 to your computer and use it in GitHub Desktop.
1st refactor of devise identity management
# refactoring of Kam Low's ::find_for_oauth
# Thanks so much for this article about managing multiple providers with omniauth
# Check it out: http://sourcey.com/rails-4-omniauth-using-devise-with-twitter-facebook-and-linkedin/
def self.find_for_oauth(auth, signed_in_resource = nil)
identity = Identity.find_for_oauth(auth)
user = signed_in_resource ? signed_in_resource : identity.user
user = find_for_oauth_email(auth) || create_for_oauth(auth) if user.nil?
update_identity(identity, user)
user
end
def self.find_for_oauth_email(auth)
email = auth.info.email if auth_email_is_verified(auth)
user = User.find_by(email: email) if email
end
def self.create_for_oauth(auth)
user = User.new(
first_name: auth.extra.raw_info.first_name || auth.info.name,
last_name: auth.extra.raw_info.last_name || auth.info.name,
password: Devise.friendly_token[0,20],
email: auth_email_is_verified(auth) ? auth.info.email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com"
)
user.skip_confirmation!
user.save!
user
end
def self.auth_email_is_verified(auth)
auth.info.email && (auth.info.verified || auth.info.verified_email)
end
def self.update_identity(identity, user)
if identity.user != user
identity.user = user
identity.save!
end
end
@benpolinsky
Copy link
Author

Not a fan of the method names... but it is a start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment