Guest User

Untitled

a guest
Mar 24th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. def self.find_for_oauth(auth, signed_in_resource = nil)
  2.  
  3. # Get the identity or create it if it does not exist
  4. identity = Identity.find_for_oauth(auth)
  5.  
  6. user = signed_in_resource ? signed_in_resource : identity.user
  7.  
  8. # Create the user if needed (if no logged in user and the identity has no user associated)
  9. if user.nil?
  10.  
  11. # Get the existing user by email if the provider gives us an email.
  12. # If no email was provided we assign a temporary email and ask the
  13. # user to verify it on the next step via UsersController.finish_signup
  14. # email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
  15. email = auth.info.email
  16. user = User.find_by(:email => email) if email
  17.  
  18. email ||= "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com"
  19. username = auth.info.nickname ? auth.info.nickname :
  20. ( auth.extra.raw_info.nickname ? auth.extra.raw_info.nickname :
  21. ( auth.extra.raw_info.username ? auth.extra.raw_info.username : "nickname: " + auth.uid) )
  22. new_username = username
  23.  
  24. # Create the user if it's a new registration.
  25. # Use default values that will be updated later
  26. if user.nil?
  27. # Control if username is taken
  28. user_same_name = User.find_by(:username => new_username)
  29. while user_same_name
  30. rnd = SecureRandom.random_number(10000).to_s
  31. new_username = username + " (" + rnd + ")"
  32. user_same_name = User.find_by(:username => new_username)
  33. end
  34. user = User.new(
  35. name: auth.extra.raw_info.name,
  36. username: new_username,
  37. email: email,
  38. password: Devise.friendly_token[0,20],
  39. )
  40. user.skip_confirmation!
  41. user.save!
  42. end
  43. end
  44.  
  45. # Associate the identity with the user if needed
  46. if identity.user != user
  47. identity.user = user
  48. identity.save!
  49. end
  50. user
  51. end
Add Comment
Please, Sign In to add comment