Skip to content

Instantly share code, notes, and snippets.

@stevepolitodesign
Last active December 14, 2021 21:50
Show Gist options
  • Save stevepolitodesign/9b92b107e76aa272a6425959b8b5b289 to your computer and use it in GitHub Desktop.
Save stevepolitodesign/9b92b107e76aa272a6425959b8b5b289 to your computer and use it in GitHub Desktop.
Use authenticate_by and not authenticate

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

Before

user = User.create(email: '[email protected]', password: 'abc123', password_confirmation: 'abc123') 
user.authenticate('abc123')
# => #<User>

After

User.create(email: '[email protected]', password: 'abc123', password_confirmation: 'abc123')
User.authenticate_by(email: '[email protected]', password: 'abc123')
# => #<User>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment