Module: MinimalistAuthentication::User::ClassMethods

Defined in:
lib/minimalist_authentication/user.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(params) ⇒ Object

Authenticates a user form the params provided. Expects a params hash with email or username and password keys. Params examples: { email: ‘[email protected]’, password: ‘abc123’ } { username: ‘user’, password: ‘abc123’ } Returns user upon successful authentication. Otherwise returns nil.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/minimalist_authentication/user.rb', line 52

def authenticate(params)
  # extract email or username and the associated value
  field, value = params.to_h.select { |key, value| %w(email username).include?(key.to_s) && value.present? }.first
  # return nil if field, value, or password is blank
  return if field.blank? || value.blank? || params[:password].blank?
  # attempt to find the user using field and value
  user = active.where(field => value).first
  # check if a user was found and if they can be authenticated
  return unless user && user.authenticated?(params[:password])
  # return the authenticated user
  return user
end

#guestObject

Returns a frozen user with the email set to GUEST_USER_EMAIL.



66
67
68
# File 'lib/minimalist_authentication/user.rb', line 66

def guest
  new(email: GUEST_USER_EMAIL).freeze
end