Sha256: 113daf32b60fb803ee796e50eb2b287a7c28bf0c31b530b6ed0a09e7aec24007

Contents?: true

Size: 1.77 KB

Versions: 9

Compression:

Stored size: 1.77 KB

Contents

  TEMP_EMAIL_PREFIX = 'change@me'
  TEMP_EMAIL_REGEX = /\Achange@me/

  has_many :identities

  # validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update

  def self.find_for_oauth(auth, signed_in_resource = nil)

    # Get the identity and user if they exist
    identity = Identity.find_for_oauth(auth)

    # If a signed_in_resource is provided it always overrides the existing user
    # to prevent the identity being locked with accidentally created accounts.
    # Note that this may leave zombie accounts (with no associated identity) which
    # can be cleaned up at a later date.
    user = signed_in_resource ? signed_in_resource : identity.user

    # Create the user if needed
    if user.nil?

      # Get the existing user by email if the provider gives us a verified email.
      # If no verified 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 if email_is_verified
      user = User.where(:email => email).first if email

      # Create the user if it's a new registration
      if user.nil?
        user = User.new(
          # name: auth.extra.raw_info.name,
          #username: auth.info.nickname || auth.uid,
          email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
          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

  def email_verified?
    self.email && self.email !~ TEMP_EMAIL_REGEX
  end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
happy_seed-0.0.11 lib/generators/happy_seed/omniauth/templates/user.rb
happy_seed-0.0.10 lib/generators/happy_seed/omniauth/templates/user.rb
happy_seed-0.0.9 lib/generators/happy_seed/omniauth/templates/user.rb
happy_seed-0.0.8 lib/generators/happy_seed/omniauth/templates/user.rb
happy_seed-0.0.7 lib/generators/happy_seed/omniauth/templates/user.rb
happy_seed-0.0.6 lib/generators/happy_seed/omniauth/templates/user.rb
happy_seed-0.0.5 lib/generators/happy_seed/omniauth/templates/user.rb
happy_seed-0.0.4 lib/generators/happy_seed/omniauth/templates/user.rb
happy_seed-0.0.3 lib/generators/happy_seed/omniauth/templates/user.rb