Module: Devision::Models::DatabaseAuthenticatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/devision/models/database_authenticatable.rb

Overview

Authenticatable Module, responsible for encrypting password and validating authenticity of a user while signing in.

Options

Examples

User.find(1).valid_password?('password123')         # returns true/false

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.required_fields(klass) ⇒ Object

Fields required on the target Model



19
20
21
# File 'lib/devision/models/database_authenticatable.rb', line 19

def self.required_fields(klass)
  [:encrypted_password] + klass.authentication_keys
end

Instance Method Details

#after_database_authenticationObject

A callback initiated after successfully authenticating. This can be used to insert your own logic that is only run after the user successfully authenticates.

Example:

def after_database_authentication
  self.update_attribute(:invite_code, nil)
end


57
58
# File 'lib/devision/models/database_authenticatable.rb', line 57

def after_database_authentication
end

#authenticatable_saltObject

A reliable way to expose the salt regardless of the implementation.



61
62
63
# File 'lib/devision/models/database_authenticatable.rb', line 61

def authenticatable_salt
  encrypted_password[0,29] if encrypted_password
end

#clean_up_passwordsObject

Set password and password confirmation to nil



43
44
45
# File 'lib/devision/models/database_authenticatable.rb', line 43

def clean_up_passwords
  self.password = self.password_confirmation = nil
end

#password=(new_password) ⇒ Object

Generates password encryption based on the given value.



29
30
31
32
# File 'lib/devision/models/database_authenticatable.rb', line 29

def password=(new_password)
  @password = new_password
  self.encrypted_password = password_digest(@password) if @password.present?
end

#valid_password?(password) ⇒ Boolean

Verifies whether an password (ie from sign in) is the user password.

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/devision/models/database_authenticatable.rb', line 35

def valid_password?(password)
  return false if encrypted_password.blank?
  bcrypt   = ::BCrypt::Password.new(encrypted_password)
  password = ::BCrypt::Engine.hash_secret("#{password}#{self.class.pepper}", bcrypt.salt)
  Devision.secure_compare(password, encrypted_password)
end