Sha256: fb8600a5b13d5392a28c5a9f692fea19618ec352c4ee7f4354e5ef94851e1814

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 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' }
    # Returns user object upon successful authentication.
    def self.authenticated_user(params)
      hash = params.to_h.with_indifferent_access

      # Extract login field from hash
      field = (hash.keys & LOGIN_FIELDS).first

      # Attempt to authenticate user
      new(field: field, value: hash[field], password: hash["password"]).authenticated_user
    end

    def initialize(field:, value:, password:)
      @field    = field
      @value    = value
      @password = password
    end

    # Returns user upon successful authentication, otherwise returns nil.
    def authenticated_user
      user if valid? && user&.authenticated?(password)
    end

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

    private

    def user
      @user ||= MinimalistAuthentication.configuration.user_model.active.find_by(field => value)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
minimalist_authentication-2.5.2 lib/minimalist_authentication/authenticator.rb
minimalist_authentication-2.5.1 lib/minimalist_authentication/authenticator.rb
minimalist_authentication-2.5.0 lib/minimalist_authentication/authenticator.rb