Class: Otp::Verify
- Inherits:
-
Object
- Object
- Otp::Verify
- Defined in:
- lib/otp/verify.rb
Overview
This module provides functionality to verify the One-Time Password (OTP). It initializes with a resource (which should respond to ‘verifications`) and an OTP. It raises errors if the resource is invalid or the OTP is not present. It provides an `authenticate` method to verify the OTP against the stored verification record. It checks if the verification record exists, if it is not expired, and if the OTP matches. If the OTP is valid, it marks the verification as verified and returns true; otherwise, it returns false.
Instance Method Summary collapse
-
#authenticate ⇒ Boolean
This method attempts to authenticate the user by verifying the provided OTP.
-
#initialize(resource, otp) ⇒ Verify
constructor
A new instance of Verify.
Constructor Details
#initialize(resource, otp) ⇒ Verify
Returns a new instance of Verify.
9 10 11 12 13 14 15 |
# File 'lib/otp/verify.rb', line 9 def initialize(resource, otp) @resource = resource @otp = otp raise Otp::Errors::InvalidResourceError unless @resource.respond_to?(:verifications) raise Otp::Errors::InvalidOtpError unless @otp.present? end |
Instance Method Details
#authenticate ⇒ Boolean
This method attempts to authenticate the user by verifying the provided OTP. It checks if the verification record exists, if it is not expired, and if the OTP matches. If the OTP is valid, it marks the verification as verified and returns true; otherwise, it returns false.
21 22 23 24 |
# File 'lib/otp/verify.rb', line 21 def authenticate @verification = @resource.verifications.first verify_code ? verify! : false end |