gpt4 book ai didi

ruby-on-rails - Rails 密码在 24 小时内过期

转载 作者:行者123 更新时间:2023-12-04 05:46:40 24 4
gpt4 key购买 nike

在我的 rails 3.1 应用程序中,我想为用户创建和过期随 secret 码。我为此使用了设计 gem。任何插件可用于 expiring password有一些持续时间?
否则请给我一些合乎逻辑的建议来实现这个功能。
请把我当作一个新手。

最佳答案

听起来您只想让密码过期一次。如果您希望定期执行此操作(例如每两个月一次),或者您想阻止用户重复使用密码,则情况会变得更加复杂。

取自我正在开发的应用程序:

app/models/user.rb(假设这是你给模型命名的):

def password_should_expire?
# your logic goes here, remember it should return false after the password has been reset
end

应用程序/ Controller /application_controller.rb
before_filter :check_password_expiry
def check_password_expiry
return if !current_user || ["sessions","passwords"].include?(controller_name)
# do nothing if not logged in, or viewing an account-related page
# otherwise you might lock them out completely without being able to change their password
if current_user.password_should_expire?
@expiring_user = current_user # save him for later
@expiring_user.generate_reset_password_token! # this is a devise method
sign_out(current_user) # log them out and force them to use the reset token to create a new password
redirect_to edit_password_url(@expiring_user, :reset_password_token => @expiring_user.reset_password_token, :forced_reset => true)
end
end

关于ruby-on-rails - Rails 密码在 24 小时内过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7649560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com