Sha256: da874ae2fed724577c6d237278720112017b33b382af3ef44daf3f678bff9b9b

Contents?: true

Size: 1.45 KB

Versions: 10

Compression:

Stored size: 1.45 KB

Contents

module Authenticate
  module Model
    #
    # Use :username as the identifier for the user. Username must be unique.
    #
    # = Columns
    # * username - the username of your user
    #
    # = Validations
    # * :username requires username is set, ensure it is unique
    #
    # = class methods
    # * credentials(params) - return the credentials required for authorization by username
    # * authenticate(credentials) - find user with given username, validate their password, return user if authenticated
    # * find_by_credentials(credentials) - find and return the user with the username in the credentials
    #
    module Username
      extend ActiveSupport::Concern

      def self.required_fields(_klass)
        [:username, :email]
      end

      included do
        # before_validation :normalize_username
        validates :username,
                  presence: true,
                  uniqueness: { allow_blank: true }
      end

      # Class methods for managing username-based authentication
      module ClassMethods
        def credentials(params)
          [params[:session][:username], params[:session][:password]]
        end

        def authenticate(credentials)
          user = find_by_credentials(credentials)
          user && user.password_match?(credentials[1]) ? user : nil
        end

        def find_by_credentials(credentials)
          username = credentials[0]
          find_by_username username
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
authenticate-0.7.3 lib/authenticate/model/username.rb
authenticate-0.7.2 lib/authenticate/model/username.rb
authenticate-0.7.1 lib/authenticate/model/username.rb
authenticate-0.7.0 lib/authenticate/model/username.rb
authenticate-0.6.1 lib/authenticate/model/username.rb
authenticate-0.6.0 lib/authenticate/model/username.rb
authenticate-0.5.0 lib/authenticate/model/username.rb
authenticate-0.4.0 lib/authenticate/model/username.rb
authenticate-0.3.3 lib/authenticate/model/username.rb
authenticate-0.3.2 lib/authenticate/model/username.rb