module Authlogic::Session::Validation

Responsible for session validation

Public Instance Methods

attempted_record() click to toggle source

You should use this as a place holder for any records that you find during validation. The main reason for this is to allow other modules to use it if needed. Take the failed_login_count feature, it needs this in order to increase the failed login count.

# File lib/authlogic/session/validation.rb, line 30
def attempted_record
  @attempted_record
end
attempted_record=(value) click to toggle source

See #attempted_record

# File lib/authlogic/session/validation.rb, line 35
def attempted_record=(value)
  @attempted_record = value
end
errors() click to toggle source

The errors in Authlogic work JUST LIKE ActiveRecord. In fact, it uses the exact same ActiveRecord errors class. Use it the same way:

Example

class UserSession
  before_validation :check_if_awesome

  private
    def check_if_awesome
      errors.add(:login, "must contain awesome") if login && !login.include?("awesome")
      errors.add(:base, "You must be awesome to log in") unless attempted_record.awesome?
    end
end
# File lib/authlogic/session/validation.rb, line 53
def errors
  @errors ||= Errors.new(self)
end
valid?() click to toggle source

Determines if the information you provided for authentication is valid or not. If there is a problem with the information provided errors will be added to the errors object and this method will return false.

# File lib/authlogic/session/validation.rb, line 60
def valid?
  errors.clear
  self.attempted_record = nil

  before_validation
  new_session? ? before_validation_on_create : before_validation_on_update
  validate
  ensure_authentication_attempted

  if errors.empty?
    new_session? ? after_validation_on_create : after_validation_on_update
    after_validation
  end

  save_record(attempted_record)
  errors.empty?
end

Private Instance Methods

ensure_authentication_attempted() click to toggle source
# File lib/authlogic/session/validation.rb, line 80
def ensure_authentication_attempted
  if errors.empty? && attempted_record.nil?
    errors.add(
      :base,
      I18n.t(
        "error_messages.no_authentication_details",
        default: "You did not provide any details for authentication."
      )
    )
  end
end