Sha256: bcc55bb837256f490e52571b4a1d345909531f56ebb9d8221e3ee0f7a6791fe7

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module Devise
  class PinfirmableController < DeviseController
    def new
      @locked_out = locked_out?
    end

    def create
      if locked_out?
        @locked_out = true
        render(:new, status: 429) && return
      end

      if Pinfirmable::Pin.new(params[:digits]).matches_user_pin(pinfirmable_user)
        pinfirmable_user.update_attribute(:pinfirmable_pin, nil)
        redirect_to after_confirmation_path_for(resource_name, pinfirmable_user)
      else
        tries = pinfirmable_user.pinfirmable_tries += 1
        lockout = (tries % 3).zero? ? (tries / 3).minute.from_now : nil
        pinfirmable_user.update_attributes(
          pinfirmable_tries: tries,
          pinfirmable_lockout: lockout
        )
        redirect_to user_confirmemail_path
      end
    end

    def resend_email
      flash[:notice] = "We have just resent your code"
      PinfirmableMailer.pin_email(pinfirmable_user).deliver
      redirect_to user_confirmemail_path
    end

    protected

    def pinfirmable_user
      request.env["CHECKING_PINFIRMABLE_PIN"] = true
      pu = current_user
      request.env["CHECKING_PINFIRMABLE_PIN"] = false
      pu
    end

    def locked_out?
      return false unless pinfirmable_user
      return false if pinfirmable_user.pinfirmable_lockout.nil?
      pinfirmable_user.pinfirmable_lockout > Time.now
    end

    def after_confirmation_path_for(resource_name, resource)
      if signed_in?(resource_name)
        signed_in_root_path(resource)
      else
        new_session_path(resource_name)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pinfirmable-0.1.0 app/controllers/devise/pinfirmable_controller.rb