hassox (owner)

Forks

Revisions

gist: 131050 Download_button fork
public
Public Clone URL: git://gist.github.com/131050.git
Ruby
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Warden::Strategies.add(:bcrypt) do
  def valid?
    params[:username] || params[:password]
  end
  
  def authenticate!
    return fail! unless user = User.first(:username => params[:username])
    
    if user.encrypted_password == params[:password]
      success!(user)
    else
      errors.add(:login, "Username or Password incorrect")
      fail!
    end
  end
end
 
 
############ The user model
 
class User < ActiveRecord::Base
  attr_accessor :password, :password_confirmation
  
  validates_present :encrypted_password
  validates_confirmation_of :password, :if => :password
  
  def password=(pass)
    @password = pass
    self.encrypted_password = pass.nil? ? nil : ::BCrypt::Password.create(pass)
  end
  
  def encrypted_password
    @encrypted_password ||= begin
      ep = read_attribute(encrypted_password)
      ep.nil? ? nil : ::BCrypt::Password.new(ep)
    end
  end
end
 
 
################ DM
class User
  attr_accessor :password, :password_confirmation
  
  include DataMapper::Resource
  
  property :id, Serial
  property :encrypted_password, BCryptHash, :nullable => false
  
  validates_is_confirmed :password
  
  def password=(pass)
    @password = pass
    self.encrypted_password = pass
  end
end