gem 'rotp'
gem 'active_model_otp'
bundle install
otp_secret_key
to the model:class AddOtpSecretKeyToUsers < ActiveRecord::Migration[5.2]
def change
add_column :washers, :otp_secret_key, :string
end
end
has_one_time_password
to model file#User.rb
class User < ApplicationRecord
has_one_time_password`
.
.
...
end
When the model is created, the otp_secret_key is saved.
user.otp_code
. This is the code you send to the user in an email or sms.user.authenticate_otp(user.otp_code, drift: 1000)
the drift
property specifies how much time you want the code to valid for in seconds
so 7_200
for 2 hours
. the default amount of time is 30 secondsuser.otp_code
user.authenticate_otp('146546')
sleep 30.seconds
user.authenticate_otp('146546')
user.authenticate_otp('146546', drift: 7_200)