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.

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.
              2.weeks by default.

remember_across_browsers: if true, a valid remember token can be
                          re-used between multiple browsers.
                          True by default.

extend_remember_period: if true, extends the user's remember period
                        when remembered via cookie.
                        False by default.

Examples:

User.find(1).remember_me!  # regenerating the token
User.find(1).forget_me!    # clearing the token

# generating info to put into cookies
User.serialize_into_cookie(user)

# lookup the user based on the incoming cookie information
User.serialize_from_cookie(cookie_string)

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details



77
78
79
# File 'lib/devise/models/rememberable.rb', line 77

def cookie_domain
  self.class.cookie_domain
end

Returns:

  • (Boolean)


81
82
83
# File 'lib/devise/models/rememberable.rb', line 81

def cookie_domain?
  self.class.cookie_domain != false
end

#forget_me!Object

Removes the remember token only if it exists, and save the record without validations.



59
60
61
62
63
64
65
# File 'lib/devise/models/rememberable.rb', line 59

def forget_me!
  if remember_token
    self.remember_token = nil
    self.remember_created_at = nil
    save(:validate => false)
  end
end

#remember_expired?Boolean

Remember token should be expired if expiration time not overpass now.

Returns:

  • (Boolean)


68
69
70
# File 'lib/devise/models/rememberable.rb', line 68

def remember_expired?
  remember_created_at && (remember_expires_at <= Time.now.utc)
end

#remember_expires_atObject

Remember token expires at created time + remember_for configuration



73
74
75
# File 'lib/devise/models/rememberable.rb', line 73

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 generate_remember_token?
  self.remember_created_at = Time.now.utc if generate_remember_timestamp?(extend_period)
  save(:validate => false)
end