user.rb 977 B

123456789101112131415161718192021222324252627282930313233343536
  1. # frozen_string_literal: true
  2. class User < ApplicationRecord
  3. has_secure_password
  4. attr_accessor :login
  5. # validates :email, confirmation: true
  6. class << self
  7. # Devise method overridden to allow sign in with email or username
  8. def find_for_database_authentication(passed_conditions)
  9. conditions = passed_conditions.dup
  10. if (login = conditions.delete(:login))
  11. where(conditions).find_by(
  12. 'lower(username) = :value OR lower(email) = :value',
  13. value: login.downcase.strip,
  14. )
  15. else
  16. find_by(conditions)
  17. end
  18. end
  19. end
  20. def self.from_omniauth(auth)
  21. where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  22. user.email = auth.info.email
  23. rlength = (20 * 3) / 4
  24. user.password = SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz')
  25. # Reddit, Google, and Discord both have email verification already
  26. # user.skip_confirmation! # FIXME
  27. end
  28. end
  29. end