对于我最新的项目,我正在使用https://github.com/hassox/rails_warden。它非常适合我的需求,除了我找不到实现remember_me的好方法。我知道从安全的角度来看,记住这一点是非常困难的,所以我希望有一个能够完成工作的项目。有人见过什么或得到一个好主意吗?
答案 0 :(得分:5)
好的,这就是我如何解决它
# User model must have remember_token attribute
# in config.ru
use Rack::Cookies
run MyApp
# in lib/strategies.rb
Strategies.add(:cookie) do
def valid?
env['rack.cookies']['user.remember.token']
end
def authenticate!
if user = User.find_by_remember_token(cookies['user.remember.token'])
success! user
else
fail! "Could not log in"
end
end
end
Manager.after_authentication :scope => :user do |user, auth, opts|
auth.env['rack.cookies']['user.remember.token'] = user.generate_remember_token! # sets its remember_token attribute to some large random value and returns the value
end
Manager.before_logout :scoper => :user do |user, auth, opts|
user.update_attribute :remember_token, nil
end
答案 1 :(得分:4)
Devise是Warden之上的身份验证解决方案,实施rememberable。