Module: Devise::Models::Rememberable
- Extended by:
- ActiveSupport::Concern
- 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.
Options
Rememberable adds the following options in devise_for:
* +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.
* +remember_across_browsers+: if a valid remember token can be re-used
between multiple browsers. By default remember_across_browsers is true
and cannot be turned off if you are using password salt instead of remember
token.
* +extend_remember_period+: if true, extends the user's remember period
when remembered via cookie. False by default.
* +cookie_options+: configuration options passed to the created cookie.
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
Instance Attribute Summary collapse
-
#extend_remember_period ⇒ Object
Returns the value of attribute extend_remember_period.
-
#remember_me ⇒ Object
Returns the value of attribute remember_me.
Instance Method Summary collapse
- #cookie_options ⇒ Object
-
#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!(extend_period = false) ⇒ Object
Generate a new remember token and save the record without validations unless remember_across_browsers is true and the user already has a valid token.
- #rememberable_value ⇒ Object
Instance Attribute Details
#extend_remember_period ⇒ Object
Returns the value of attribute extend_remember_period.
47 48 49 |
# File 'lib/devise/models/rememberable.rb', line 47 def extend_remember_period @extend_remember_period end |
#remember_me ⇒ Object
Returns the value of attribute remember_me.
47 48 49 |
# File 'lib/devise/models/rememberable.rb', line 47 def remember_me @remember_me end |
Instance Method Details
#cookie_options ⇒ Object
87 88 89 |
# File 'lib/devise/models/rememberable.rb', line 87 def self.class. end |
#forget_me! ⇒ Object
Removes the remember token only if it exists, and save the record without validations.
59 60 61 62 63 |
# File 'lib/devise/models/rememberable.rb', line 59 def forget_me! self.remember_token = nil if respond_to?(:remember_token=) self.remember_created_at = nil save(:validate => false) end |
#remember_expired? ⇒ Boolean
Remember token should be expired if expiration time not overpass now.
66 67 68 |
# File 'lib/devise/models/rememberable.rb', line 66 def remember_expired? remember_created_at.nil? || (remember_expires_at <= Time.now.utc) end |
#remember_expires_at ⇒ Object
Remember token expires at created time + remember_for configuration
71 72 73 |
# File 'lib/devise/models/rememberable.rb', line 71 def remember_expires_at remember_created_at + self.class.remember_for end |
#remember_me!(extend_period = false) ⇒ Object
Generate a new remember token and save the record without validations unless remember_across_browsers is true and the user already has a valid token.
51 52 53 54 55 |
# File 'lib/devise/models/rememberable.rb', line 51 def remember_me!(extend_period=false) self.remember_token = self.class.remember_token if respond_to?(:remember_token) && generate_remember_token? self.remember_created_at = Time.now.utc if (extend_period) save(:validate => false) end |
#rememberable_value ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/devise/models/rememberable.rb', line 75 def rememberable_value if respond_to?(:remember_token) remember_token elsif respond_to?(:authenticatable_salt) && (salt = authenticatable_salt) salt else raise "The #{self.class.name} class does not respond to remember_token and " << "authenticatable_salt returns nil. In order to use rememberable, you must " << "add a remember_token field to your model or ensure a password is always set." end end |