aboutsummaryrefslogtreecommitdiff
path: root/interfaces/ruby/models/user.rb
blob: a251dec7708d366008523ab997007f5b59ac380b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require 'bcrypt'

class User < ActiveRecord::Base
  has_many :posts
  has_many :sessions
  
  validates_presence_of :username
  validates_presence_of :password_hash
  
  def hash(password)
    BCrypt::Password.create(password)
  end
  
  def authenticate(password)
    BCrypt::Password.new(@password) == password
  end
end

class CreateUserTable < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      t.string :name
      t.string :username, null: false, unique: true, index: true
      t.string :password_hash, null: false
      t.timestamps
    end
  end
end