Skip to content

Instantly share code, notes, and snippets.

@ajmalafif
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save ajmalafif/67c45d225acedbb394b2 to your computer and use it in GitHub Desktop.

Select an option

Save ajmalafif/67c45d225acedbb394b2 to your computer and use it in GitHub Desktop.
def self.find_for_oauth(auth, signed_in_resource = nil)
# Get the identity and user if they exist
identity = Identity.find_for_oauth(auth)
user = identity.user
user = signed_in_resource if user.nil?
if user.nil?
# Get the existing user by email if the OAuth provider gives us a verified email
# If the email has not been verified yet we will force the user to validate it
email = auth.info.email if auth.info.email && auth.info.verified_email
user = User.where(:email => email).first if email
# Create the user if it is a new registration
if user.nil?
names = auth.extra.raw_info.name.gsub(/\s+/m, ' ').strip.split(" ")
user = User.new(
first_name: names.first,
last_name: names.last,
#username: auth.info.nickname || auth.uid,
email: email ? email : TEMP_EMAIL,
password: Devise.friendly_token[0,20]
)
user.skip_confirmation!
user.save!
end
end
# Associate the identity with the user if not already
if identity.user != user
identity.user = user
identity.save!
end
user
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment