Skip to content

Instantly share code, notes, and snippets.

@mateuszbialowas
Last active June 6, 2022 14:13
Show Gist options
  • Save mateuszbialowas/9b1cab3855a85c4a5c75ead28873e25f to your computer and use it in GitHub Desktop.
Save mateuszbialowas/9b1cab3855a85c4a5c75ead28873e25f to your computer and use it in GitHub Desktop.
authenticate method
def authenticate(*credentials, &block)
raise ArgumentError, 'at least 2 arguments required' if credentials.size < 2
if credentials[0].blank?
return authentication_response(return_value: false, failure: :invalid_login, &block)
end
if @sorcery_config.downcase_username_before_authenticating
credentials[0].downcase!
end
user = sorcery_adapter.find_by_credentials(credentials)
unless user
return authentication_response(failure: :invalid_login, &block)
end
set_encryption_attributes
if user.respond_to?(:active_for_authentication?) && !user.active_for_authentication?
return authentication_response(user: user, failure: :inactive, &block)
end
@sorcery_config.before_authenticate.each do |callback|
success, reason = user.send(callback)
unless success
return authentication_response(user: user, failure: reason, &block)
end
end
unless user.valid_password?(credentials[1])
return authentication_response(user: user, failure: :invalid_password, &block)
end
authentication_response(user: user, return_value: user, &block)
end
def authentication_response(options = {})
yield(options[:user], options[:failure]) if block_given?
options[:return_value]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment