Sha256: 33e3fc6f5198ba447605fe7c83cb44a9c80b499b7498e598b49cc2ba1f969b9d

Contents?: true

Size: 1.87 KB

Versions: 4

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module MinimalistAuthentication
  class Authenticator
    LOGIN_FIELDS = %w[email username].freeze

    attr_reader :field, :value, :password

    # Attempts to find and authenticate a user based on the provided params. Expects a params
    # hash with email or username and password keys. Returns user upon successful authentication.
    # Otherwise returns nil.
    #
    # Params examples:
    # { email: 'user@example.com', password: 'abc123' }
    # { username: 'user', password: 'abc123' }
    def self.authenticate(params)
      hash = params.to_h.with_indifferent_access
      field, value = hash.find { |key, _| LOGIN_FIELDS.include?(key) }
      new(field:, value:, password: hash["password"]).authenticated_user
    end

    def self.authenticated_user(params)
      MinimalistAuthentication.deprecator.warn(<<-MSG.squish)
        Calling MinimalistAuthentication::Authenticator.authenticated_user is deprecated.
        Use MinimalistAuthentication::Authenticator.authenticate instead.
      MSG
      authenticate(params)
    end

    # Initializes a new Authenticator instance with the provided field, value, and password.
    def initialize(field:, value:, password:)
      @field    = field
      @value    = value
      @password = password
    end

    # Returns an authenticated and enabled user or nil.
    def authenticated_user
      authenticated&.enabled if valid?
    end

    private

    # Attempts to authenticate a user based on the provided field, value, and password.
    # Returns user upon successful authentication, otherwise returns nil.
    def authenticated
      MinimalistAuthentication.configuration.user_model.authenticate_by(field => value, password:)
    end

    # Returns true if all the authentication attributes are present.
    # Otherwise returns false.
    def valid?
      [field, value, password].all?(&:present?)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
minimalist_authentication-3.2.2 lib/minimalist_authentication/authenticator.rb
minimalist_authentication-3.2.1 lib/minimalist_authentication/authenticator.rb
minimalist_authentication-3.2.0 lib/minimalist_authentication/authenticator.rb
minimalist_authentication-3.1.0 lib/minimalist_authentication/authenticator.rb