Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def self.find_for_oauth(auth, signed_in_resource = nil)
- # Get the identity or create it if it does not exist
- identity = Identity.find_for_oauth(auth)
- user = signed_in_resource ? signed_in_resource : identity.user
- # Create the user if needed (if no logged in user and the identity has no user associated)
- if user.nil?
- # Get the existing user by email if the provider gives us an email.
- # If no email was provided we assign a temporary email and ask the
- # user to verify it on the next step via UsersController.finish_signup
- # email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
- email = auth.info.email
- user = User.find_by(:email => email) if email
- email ||= "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com"
- username = auth.info.nickname ? auth.info.nickname :
- ( auth.extra.raw_info.nickname ? auth.extra.raw_info.nickname :
- ( auth.extra.raw_info.username ? auth.extra.raw_info.username : "nickname: " + auth.uid) )
- new_username = username
- # Create the user if it's a new registration.
- # Use default values that will be updated later
- if user.nil?
- # Control if username is taken
- user_same_name = User.find_by(:username => new_username)
- while user_same_name
- rnd = SecureRandom.random_number(10000).to_s
- new_username = username + " (" + rnd + ")"
- user_same_name = User.find_by(:username => new_username)
- end
- user = User.new(
- name: auth.extra.raw_info.name,
- username: new_username,
- email: email,
- password: Devise.friendly_token[0,20],
- )
- user.skip_confirmation!
- user.save!
- end
- end
- # Associate the identity with the user if needed
- if identity.user != user
- identity.user = user
- identity.save!
- end
- user
- end
Add Comment
Please, Sign In to add comment