Skip to content

Instantly share code, notes, and snippets.

@yannvery
Created December 4, 2014 14:14
Show Gist options
  • Select an option

  • Save yannvery/e63bf98c8f1dc680373e to your computer and use it in GitHub Desktop.

Select an option

Save yannvery/e63bf98c8f1dc680373e to your computer and use it in GitHub Desktop.
Fix : Login by username and reset password on Devise
# Devise 3.4.1 and Rails 4.1.7 be carefull if you apply this method to authorize users to log in with username or email : https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address
# Add this method on User model file
def self.find_first_by_auth_conditions(warden_conditions)
# Note to_h method this must be used instead, reset password will failed
# Future pull on devise will fix it : https://github.com/plataformatec/devise/pull/3162
conditions = warden_conditions.dup.to_h
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment