I've been following this tutorial to make an AJAX login form: http://www.mysite4u.net/joomla-articles/jquery-ajax-joomla-login.html
But I can't get it to work. The ajax call doesn't work. It gives me this error:
Uncaught TypeError: Cannot read property 'loggedIn' of null
I tried reading this documentation, but I guess that it is not the AJAX component that is used in this, and it doesn't mention anything about calling components: https://docs.joomla.org/Using_Joomla_Ajax_Interface
This is my AJAX call:
form = jQuery('#userForm');
jQuery.ajax({
url: 'index.php?option=com_content&format=ajax&view=article&task=loginUser&tmpl=component',
type: 'post',
dataType: 'html',
data: {
form: jQuery(form).serializeArray()
},
async: true,
success: function(response){
console.log(response);
var result = jQuery.parseJSON(response);
if ( result.loggedIn == 1 ) {
//User logged in
}
else {
//User not logged in
}
}
});
And the function inside components/com_content/views/article/view.ajax.php
("view.ajax.php" is a file I've created):
public function _logInUser( $form ){
$options = array();
$credentials = array();
$credentials['username'] = $form[0]['value'];
$credentials['password'] = $form[1]['value'];
$result = JFactory::getApplication()->login($credentials, $options);
$result = ($result) ? 1 : 0;
//1 - logged in
//0 - not logged in
echo json_encode( array('loggedIn' => $result) );
jexit();
}