Sha256: 81428497ab9e3edab833a56051d99cd20917bd9d6c8d81d2024480b54e38757b

Contents?: true

Size: 926 Bytes

Versions: 3

Compression:

Stored size: 926 Bytes

Contents

module Nyauth
  module ResetPasswordAbility
    extend ActiveSupport::Concern

    included do
      before_validation :check_reset_password_key, on: :reset_password
      validates :password, presence: true,
                           length: { minimum: Nyauth.configuration.password_minium },
                           on: [:create, :update_password, :reset_password]
      validates :password, confirmation: true
    end

    def reset_password(params)
      self.attributes = params
      self.save(context: :reset_password)
    end

    def request_reset_password
      self.reset_password_key = SecureRandom.hex(32)
      self.reset_password_key_expired_at = Time.current + 1.hour
      save
    end

    private

    def check_reset_password_key
      if reset_password_key_expired_at.past?
        errors.add(:reset_password_key, :expired)
      else
        self.reset_password_key = nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nyauth-0.2.2 app/models/concerns/nyauth/reset_password_ability.rb
nyauth-0.2.1 app/models/concerns/nyauth/reset_password_ability.rb
nyauth-0.2.0 app/models/concerns/nyauth/reset_password_ability.rb