Class: User

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
lib/templates/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



23
24
25
# File 'lib/templates/user.rb', line 23

def password
  @password
end

Class Method Details

.find_by_credentials(username, password) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/templates/user.rb', line 13

def self.find_by_credentials(username, password)
  user = User.find_by(username: username)
  return nil if user.nil?
  if user.is_password?(password)
    return user
  else
    return nil
  end
end

.tokenObject



9
10
11
# File 'lib/templates/user.rb', line 9

def self.token
  SecureRandom::urlsafe_base64(16)
end

Instance Method Details

#digest(string) ⇒ Object



30
31
32
# File 'lib/templates/user.rb', line 30

def digest(string)
  string_digest = BCrypt::Password.create(string)
end

#is_password?(password) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/templates/user.rb', line 34

def is_password?(password)
  BCrypt::Password.new(self.password_digest).is_password?(password)
end

#reset_session_token!Object



38
39
40
41
42
# File 'lib/templates/user.rb', line 38

def reset_session_token!
  self.session_token = User.token
  self.save
  self.session_token
end