module Composable module Pwdless module Form class Verification < Composable::Form::Command attribute :salt, :data, :code validates :data, :code, presence: true validates :secret, presence: true, if: -> { data? && code? } after_save { secret.decrement_remaining_attempts! } def save errors.add(:code, :invalid) unless secret.valid? end def invalid_code? !code? || errors.added?(:code, :invalid) end private delegate :has_expired?, :has_exceeded_attempts?, to: :secret, allow_nil: true def secret return @secret if defined?(@secret) @secret = Pwdless::Secret.find_by_digest(salt: salt, data: data).tap do |secret| secret.code = code if secret end end end end end end