Sha256: bf901057d4f7aa5da8024c2a1a1802e76f1c1e3b73eeeeaa8b9bee5e839f3367

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module AuthN
  module Model
    def self.included(object)
      object.extend ClassMethods
    end

    module ClassMethods
      def has_authentication(o = {})
        options = AuthN.config.table.any? ? o.merge!(AuthN.config) : o
        config.merge! options
      end

      def config=(options)
        @@config = options
      end

      def config
        @@config ||= AuthN::Config.new
      end

      def authenticate(identifiers = {})
        # Extract the password from the identifiers
        password = identifiers.delete AuthN.config.login_password_key

        # Find the documents that match the criteria
        criteria = send AuthN.config.model_critera_method, identifiers

        # Get the first document that matches the criteria
        instance = criteria.first

        # Check to see if the instance exists and if a password was given
        if instance && password
          # Check to see if the instance can authenticate with password
          if instance.authenticate password
            instance
          else
            false
          end
        end

        # Return instance if account was found and password matched
        # Return false if account was found and password didn't match
        # Return nil if account wasn't found OR password wasn't given
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authn-1.0.0 lib/authn/model.rb