class MinimalistAuthentication::Authenticator

Constants

LOGIN_FIELDS

Attributes

field[R]
password[R]
value[R]

Public Class Methods

authenticate(params) click to toggle source

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’ }

# File lib/minimalist_authentication/authenticator.rb, line 16
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
authenticated_user(params) click to toggle source
# File lib/minimalist_authentication/authenticator.rb, line 22
    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
new(field:, value:, password:) click to toggle source

Initializes a new Authenticator instance with the provided field, value, and password.

# File lib/minimalist_authentication/authenticator.rb, line 31
def initialize(field:, value:, password:)
  @field    = field
  @value    = value
  @password = password
end

Public Instance Methods

authenticated_user() click to toggle source

Returns an authenticated and enabled user or nil.

# File lib/minimalist_authentication/authenticator.rb, line 38
def authenticated_user
  authenticated&.enabled if valid?
end

Private Instance Methods

authenticated() click to toggle source

Attempts to authenticate a user based on the provided field, value, and password. Returns user upon successful authentication, otherwise returns nil.

# File lib/minimalist_authentication/authenticator.rb, line 46
def authenticated
  MinimalistAuthentication.configuration.user_model.authenticate_by(field => value, password:)
end
valid?() click to toggle source

Returns true if all the authentication attributes are present. Otherwise returns false.

# File lib/minimalist_authentication/authenticator.rb, line 52
def valid?
  [field, value, password].all?(&:present?)
end