Module: Devise::Models::Rememberable
- Defined in:
- lib/devise/models/rememberable.rb
Overview
Rememberable manages generating and clearing token for remember the user from a saved cookie. Rememberable also has utility methods for dealing with serializing the user into the cookie and back from the cookie, trying to lookup the record based on the saved information. You probably wouldn’t use rememberable methods directly, they are used mostly internally for handling the remember token.
Configuration:
remember_for: the time you want the user will be remembered without
asking for credentials. After this time the user will be
blocked and will have to enter his credentials again.
This configuration is also used to calculate the expires
time for the cookie created to remember the user.
By default remember_for is 2.weeks.
Examples:
User.find(1).remember_me! # regenerating the token
User.find(1).forget_me! # clearing the token
# generating info to put into cookies
User.(user)
# lookup the user based on the incoming cookie information
User.()
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#forget_me! ⇒ Object
Removes the remember token only if it exists, and save the record without validations.
-
#remember_expired? ⇒ Boolean
Remember token should be expired if expiration time not overpass now.
-
#remember_expires_at ⇒ Object
Remember token expires at created time + remember_for configuration.
-
#remember_me! ⇒ Object
Generate a new remember token and save the record without validations.
-
#valid_remember_token?(token) ⇒ Boolean
Checks whether the incoming token matches or not with the record token.
Class Method Details
.included(base) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/devise/models/rememberable.rb', line 34 def self.included(base) base.class_eval do extend ClassMethods # Remember me option available in after_authentication hook. attr_accessor :remember_me end end |
Instance Method Details
#forget_me! ⇒ Object
Removes the remember token only if it exists, and save the record without validations.
53 54 55 56 57 58 59 |
# File 'lib/devise/models/rememberable.rb', line 53 def forget_me! if remember_token self.remember_token = nil self.remember_created_at = nil save(false) end end |
#remember_expired? ⇒ Boolean
Remember token should be expired if expiration time not overpass now.
67 68 69 |
# File 'lib/devise/models/rememberable.rb', line 67 def remember_expired? remember_expires_at <= Time.now.utc end |
#remember_expires_at ⇒ Object
Remember token expires at created time + remember_for configuration
72 73 74 75 |
# File 'lib/devise/models/rememberable.rb', line 72 def remember_expires_at # TODO: with MongoId need transform by remember_created_at.utc remember_created_at + self.class.remember_for end |
#remember_me! ⇒ Object
Generate a new remember token and save the record without validations.
44 45 46 47 48 49 |
# File 'lib/devise/models/rememberable.rb', line 44 def remember_me! self.remember_token = Devise.friendly_token self.remember_created_at = Time.now.utc save(false) end |
#valid_remember_token?(token) ⇒ Boolean
Checks whether the incoming token matches or not with the record token.
62 63 64 |
# File 'lib/devise/models/rememberable.rb', line 62 def valid_remember_token?(token) remember_token && !remember_expired? && remember_token == token end |