gpt4 book ai didi

ruby-on-rails - 如何使用 Devise 锁定用户?

转载 作者:行者123 更新时间:2023-12-04 03:01:34 25 4
gpt4 key购买 nike

我想在我的应用程序中为帐户持有人用户添加订阅类型功能,这样即使尝试登录失败,他们也将无法访问他们的帐户。注意:我不想从数据库中删除他们的帐户。我已经安装了 devise-2.1.2在我的应用程序中。有没有人知道如何做到这一点?我是新手 Ruby on rails所以如果你解释一下这些步骤,这对我会很有帮助。

最佳答案

设计具有内置解决方案 :lockable选项检查 Devise Lockable Documentation

您必须设置 lock_strategy设置为 :failed_attempts.
第1步
设置你的 config/initializers/devise.rb 使用:

# Defines which strategy will be used to lock an account.
config.lock_strategy = :failed_attempts

# Defines which key will be used when locking and unlocking an account
config.unlock_keys = [ :time ]

# Defines which strategy will be used to unlock an account.
# :time = Re-enables login after a certain amount of time (see :unlock_in below)
config.unlock_strategy = :time

# Number of authentication tries before locking an account if lock_strategy
# is failed attempts.
config.maximum_attempts = 3

# Time interval to unlock the account if :time is enabled as unlock_strategy.
config.unlock_in = 2.hours

第2步
您应该将可锁定的模型添加到您的模型中:
class Example < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:lockable

第 3 步
生成迁移以使设计工作
class AddLockableToExamples < ActiveRecord::Migration
def change
add_column :examples, :failed_attempts, :integer, default: 0
add_column :examples, :unlock_token, :string
add_column :examples, :locked_at, :datetime
end
end

问候!!

关于ruby-on-rails - 如何使用 Devise 锁定用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13184514/

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