Rails 7 introduces the authenticate_by
class method which prevents timing-based enumeration attacks.
This is an alternative to calling authenticate
on an instance of a User
.
class User < ActiveRecord::Base
has_secure_password
end
user = User.create(email: '[email protected]', password: 'abc123', password_confirmation: 'abc123')
user.authenticate('abc123')
# => #<User>
User.create(email: '[email protected]', password: 'abc123', password_confirmation: 'abc123')
User.authenticate_by(email: '[email protected]', password: 'abc123')
# => #<User>