How to block users using Devise?

I want to add subscription type functionality to my application for user accounts, so that after a certain period of time they will not be able to access their account? Note. I do not want to delete their account from the database. I have already installed devise-2.1.2 in my application. Does any body have any ideas how to do this? I am new to Ruby on rails , so it will be very useful for me if you explain how to do this.

+8
ruby-on-rails authorization devise
source share
1 answer

The developer has a built-in solution with the option :lockable in Lock locked documentation

You must set the lock_strategy parameter to :failed_attempts.

Step 1 Configure / initializers / devise.rb to use:

 # 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 

Step 2 For this you should add a lockable model:

 class Example < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :lockable 

Step 3 Create a migration to do development work

 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 

Best wishes!!

+18
source share

All Articles