Manfred (owner)

Forks

Revisions

gist: 106471 Download_button fork
public
Public Clone URL: git://gist.github.com/106471.git
Embed All Files: show embed
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
require 'digest/sha1'
 
class Member
  attr_reader :password
  
  def password=(password)
    self.hashed_password = self.class.hash_password(password)
  end
  
  def self.hash_password(password)
    ::Digest::SHA1.hexdigest(password)
  end
  
  # Authenticates credentials. Takes a hash with a :email and :password, returns an instance of Member.
  # The Member has errors on base when the user isn't authenticated.
  def self.authenticate(params={})
    unless member = find_by_email_and_hashed_password(params[:email], hash_password(params[:password]))
      member = Member.new
      member.errors.add_to_base("Login failed; please check email and password and try again.")
      member
    else
      member
    end
  end
end