Class: RailsBase::Authentication::AuthenticateUser

Inherits:
ServiceBase
  • Object
show all
Defined in:
app/services/rails_base/authentication/authenticate_user.rb

Instance Method Summary collapse

Methods inherited from ServiceBase

inherited, #internal_validate, #service_base_logging

Methods included from ServiceLogging

#aletered_message, #class_name, #log, #log_prefix, #logger, #service_id

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/services/rails_base/authentication/authenticate_user.rb', line 7

def call
	user = current_user || User.find_for_authentication(email: email)
	valid = user.present? && user.valid_password?(password)
	if valid
		log(level: :info, msg: "Correctly found valid user_id #{user.id}")
		context.user = user
	else
		log(level: :warn, msg: "Failed to validate credentials")
		log(level: :warn, msg: "Found user? #{user.present?}. Valid password?[#{user&.valid_password?(password)}]")
		context.fail!(message: "Incorrect credentials. Please try again")
	end
end

#validate!Object



20
21
22
23
24
25
26
# File 'app/services/rails_base/authentication/authenticate_user.rb', line 20

def validate!
	raise "Expected email to be a String. Received #{email.class}" unless email.is_a? String
	raise "Expected password to be a String. Received #{password.class}" unless password.is_a? String

	return unless current_user
	raise "Expected current_user to be a User. Received #{current_user.class}" unless current_user.is_a? User
end