1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/lib/devise/strategies/rememberable.rb

56 lines
1.5 KiB
Ruby
Raw Normal View History

2012-03-31 07:17:16 -04:00
require 'devise/strategies/authenticatable'
2010-01-14 09:47:14 -05:00
module Devise
module Strategies
# Remember the user through the remember token. This strategy is responsible
# to verify whether there is a cookie with the remember token, and to
# recreate the user from this cookie if it exists. Must be called *before*
2010-01-14 09:47:14 -05:00
# authenticatable.
class Rememberable < Authenticatable
2010-01-14 09:47:14 -05:00
# A valid strategy for rememberable needs a remember token in the cookies.
def valid?
@remember_cookie = nil
remember_cookie.present?
2010-01-14 09:47:14 -05:00
end
# To authenticate a user we deserialize the cookie and attempt finding
# the record in the database. If the attempt fails, we pass to another
# strategy handle the authentication.
def authenticate!
resource = mapping.to.serialize_from_cookie(*remember_cookie)
unless resource
cookies.delete(remember_key)
return pass
end
if validate(resource)
2010-01-14 09:47:14 -05:00
success!(resource)
end
end
private
2011-02-24 15:55:41 -05:00
def decorate(resource)
super
resource.extend_remember_period = mapping.to.extend_remember_period if resource.respond_to?(:extend_remember_period=)
end
def remember_me?
true
end
def remember_key
mapping.to.rememberable_options.fetch(:key, "remember_#{scope}_token")
end
def remember_cookie
@remember_cookie ||= cookies.signed[remember_key]
end
2010-01-14 09:47:14 -05:00
end
end
end
Warden::Strategies.add(:rememberable, Devise::Strategies::Rememberable)