I’m trying to create a simple Ajax Login system with WordPress.
Unfortunately, every time the “wp_signon” function is fired, my system failed and the only information I have is this one :
POST myurl/wp-admin/admin-ajax.php - 302 Found
GET myurl/?login = failed - 200 Found
So, whether I try to log in with the good informations or not, my JS script goes in the “error part” of my Ajax function.
Can anyone tell me what I a doing wrong? It will be much appreciated!
Many thanks!
JS :
jQuery(document).on('submit', loginForm, function(event) {
event.preventDefault();
var usernameVal = jQuery('.modal-login .login-form #user_login').val();
var passwordVal = jQuery('.modal-login .login-form #user_pass').val();
var remembermeField = jQuery('.modal-login .login-form #rememberme');
var securityVal = jQuery('.modal-login .login-form #security').val();
if ( remembermeField.prop('checked') ) {
var remembermeVal = 'true';
} else {
var remembermeVal = 'false';
}
jQuery.ajax({
type : "post",
url : data_front_end_users_management.ajaxurl,
dataType: 'json',
data: {
action: 'AuthUserAjax',
username: usernameVal,
password: passwordVal,
rememberme: remembermeVal,
security: securityVal
},
success: function(data) {
if ( data['answer'] === 1 ) {
modalAlert( msgRow, msgAlert, msgContent, false, 'alert-success', 'alert-danger', data['message'] );
} else {
modalAlert( msgRow, msgAlert, msgContent, false, 'alert-danger', 'alert-success', data['message'] );
}
//END success
},
error: function () {
modalAlert( msgRow, msgAlert, msgContent, false, 'alert-danger', 'alert-success', defaultError );
}
//END ajax
});
//END jQuery(document).on('submit', loginForm, function(event)
});
ACTION :
function AuthUserAjax() {
$LoginController = new FELoginController();
$value = array();
$value['answer'] = 0;
$value['message'] = '';
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$rememberme = $_POST['rememberme'];
$security = $_POST['security'];
if ( $LoginController->checkAuthInfosFilled( $username, $password ) ) {
$user = $LoginController->UserAuth( $username, $password, $rememberme );
if ( $user ) {
if ( is_wp_error( $user ) ) {
$value['answer'] = 0;
$value['message'] = $user->get_error_message();
} else {
$value['answer'] = 1;
$value['message'] = 'Success.';
}
//END if ( $user )
} else {
$value['answer'] = 0;
$value['message'] = 'Wrong username or password.';
}
} else {
$value['answer'] = 0;
$value['message'] = 'Please, enter your username and your password.';
}
$data = json_encode($value);
die( $data );
//END AuthUserAjax
}
add_action( 'wp_ajax_AuthUserAjax','AuthUserAjax' );
add_action( 'wp_ajax_nopriv_AuthUserAjax','AuthUserAjax' );
CONTROLLER :
public function UserAuth( $username, $password, $rememberme ) {
if ( check_ajax_referer( 'ajax-login-nonce', 'security' ) ) {
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
if ( $rememberme == 'true' || $rememberme == true ) {
$creds['remember'] = true;
} else {
$creds['remember'] = false;
}
$user = wp_signon( $creds, false );
return $user;
} else {
return false;
}
//END UserAuth
}
So, after hours of searching, I finally found where was my problem : another method in my class interfered with my ajax authentication system.