<commit_before>class User
  include Mongoid::Document
  include Mongoid::Timestamps

  field :username, type: String
  field :password, type: String
  field :role, type: String
  field :email, type: String
  field :phone, type: String
  validates_presence_of( :username, :password)

  embeds_one :shop
  has_many :foods

  class << self
    def authenticate (username, password)
      user = find_by(username: username, password: password)
    end
  end

end<commit_msg>[gongming] #401 BCrypt user password to secret hash in database<commit_after>class User
  include Mongoid::Document
  include Mongoid::Timestamps

  field :username, type: String
  field :password_hash, type: String
  field :password_salt, type: String
  field :role, type: String
  field :email, type: String
  field :phone, type: String

  attr_accessor :password

  validates_presence_of( :username, :password)

  embeds_one :shop
  has_many :foods
  before_save :encrypt_password

  class << self
    def authenticate (username, password)
      user = find_by(username: username)
      if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
        user
      end
    end
  end

  private

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end

end
{'fraction_non_alphanumeric': 0.08071428571428571, 'fraction_numerical': 0.002142857142857143, 'mean_word_length': 3.950530035335689, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1954766', 'n_tokens_mistral': 444, 'n_tokens_neox': 420, 'n_words': 127}