Module: Devise::Models::Authenticatable
- Included in:
- Lockable
- Defined in:
- lib/devise/models/authenticatable.rb
Overview
Authenticable Module, responsible for encrypting password and validating authenticity of a user while signing in.
Configuration:
You can overwrite configuration values by setting in globally in Devise, using devise method or overwriting the respective instance method.
pepper: encryption key used for creating encrypted password. Each time
password changes, it's gonna be encrypted again, and this key
is added to the password and salt to create a secure hash.
Always use `rake secret' to generate a new key.
stretches: defines how many times the password will be encrypted.
encryptor: the encryptor going to be used. By default :sha1.
authentication_keys: parameters used for authentication. By default [:email]
Examples:
User.authenticate('[email protected]', 'password123') # returns authenticated user or nil
User.find(1).valid_password?('password123') # returns true/false
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#password=(new_password) ⇒ Object
Regenerates password salt and encrypted password each time password is set.
-
#update_with_password(params = {}) ⇒ Object
Update record attributes when :old_password matches, otherwise returns error on :old_password.
-
#valid_for_authentication?(attributes) ⇒ Boolean
Checks if a resource is valid upon authentication.
-
#valid_password?(incoming_password) ⇒ Boolean
Verifies whether an incoming_password (ie from sign in) is the user password.
Class Method Details
.included(base) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/devise/models/authenticatable.rb', line 30 def self.included(base) base.class_eval do extend ClassMethods attr_reader :password, :old_password attr_accessor :password_confirmation end end |
Instance Method Details
#password=(new_password) ⇒ Object
Regenerates password salt and encrypted password each time password is set.
40 41 42 43 44 45 46 47 |
# File 'lib/devise/models/authenticatable.rb', line 40 def password=(new_password) @password = new_password if @password.present? self.password_salt = self.class.encryptor_class.salt self.encrypted_password = password_digest(@password) end end |
#update_with_password(params = {}) ⇒ Object
Update record attributes when :old_password matches, otherwise returns error on :old_password.
61 62 63 64 65 66 67 68 |
# File 'lib/devise/models/authenticatable.rb', line 61 def update_with_password(params={}) if valid_password?(params[:old_password]) update_attributes(params) else self.class.add_error_on(self, :old_password, :invalid, false) false end end |
#valid_for_authentication?(attributes) ⇒ Boolean
Checks if a resource is valid upon authentication.
55 56 57 |
# File 'lib/devise/models/authenticatable.rb', line 55 def valid_for_authentication?(attributes) valid_password?(attributes[:password]) end |
#valid_password?(incoming_password) ⇒ Boolean
Verifies whether an incoming_password (ie from sign in) is the user password.
50 51 52 |
# File 'lib/devise/models/authenticatable.rb', line 50 def valid_password?(incoming_password) password_digest(incoming_password) == encrypted_password end |