diff --git a/app/models/user.rb b/app/models/user.rb index 989e2c05a40ee1d1a645187150c37629a58f9ba7..b96957d3c7f6b789bffaf58d63b804ce43d27bbe 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,6 +19,17 @@ class User < ApplicationRecord after_initialize :set_avatar_color + # Custom method to allow user to sign-in with any case while still storing the "correct" case in the database + def self.find_for_database_authentication(warden_conditions) + conditions = warden_conditions.dup + if (login = conditions.delete(:name)) + where(conditions.to_h).where(["lower(name) = :value", { value: login.downcase }]).first + else + logger.warn("Authentication did not query :name as expected, login will only work with exact case!") + where(conditions.to_h).first + end + end + def errors super.tap { |errors| errors.delete(:password, :blank) if password.nil? } end diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 5bb8a009b785b735d4a413180355df86cb763199..64ed76ca7672b14a1810ea55141ee02bc8836811 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -58,12 +58,12 @@ Devise.setup do |config| # Configure which authentication keys should be case-insensitive. # These keys will be downcased upon creating or modifying a user and when used # to authenticate or find a user. Default is :email. - config.case_insensitive_keys = [:name] + config.case_insensitive_keys = [:email] # Configure which authentication keys should have whitespace stripped. # These keys will have whitespace before and after removed upon creating or # modifying a user and when used to authenticate or find a user. Default is :email. - config.strip_whitespace_keys = [:name] + config.strip_whitespace_keys = [:email, :name] # Tell if authentication through request.params is enabled. True by default. # It can be set to an array that will enable params authentication only for the