Class: CowAuth::User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/cow_auth/user.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_with_password(password) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/cow_auth/user.rb', line 15

def authenticate_with_password(password)
  return false if self.encrypted_password.blank?
  if SCrypt::Password.new(self.encrypted_password) == password
    self.update(sign_in_count: self. + 1)
    return true
  end
  return false
end

#authenticate_with_token(auth_token) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/cow_auth/user.rb', line 24

def authenticate_with_token(auth_token)
  if self.auth_token.present? &&
      self.expires_at.present? &&
      self.auth_token == auth_token &&
      self.expires_at > Time.zone.now
    return true
  end
  return false
end

#create_auth_tokenObject



34
35
36
37
38
39
40
# File 'lib/cow_auth/user.rb', line 34

def create_auth_token
  self.update(
    auth_token: self.token_valid? ? self.auth_token : self.generate_auth_token,
    expires_at: self.generate_token_expires_at
  )
  return true
end

#destroy_auth_tokenObject



42
43
44
45
46
47
48
# File 'lib/cow_auth/user.rb', line 42

def destroy_auth_token
  self.update(
    auth_token: nil,
    expires_at: nil
  )
  return true
end

#password=(new_password) ⇒ Object



50
51
52
53
54
55
# File 'lib/cow_auth/user.rb', line 50

def password=(new_password)
  return false if new_password.blank?
  salt = SCrypt::Engine.generate_salt
  self.encrypted_password = SCrypt::Engine.hash_secret(new_password, salt)
  return true
end