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
}
  • Where you have code the AuthUserAjax() function – Mitul Jul 2 '15 at 6:50
  • The AuthUserAjax() function is in a file included in my plugin, call in the main file like this : require_once( FEPLUGIN_DIR . '/inc/ajax-functions.inc.php'); – Lancelot Jul 2 '15 at 6:55
  • In the plugin is activated and you want to use that shortcode in any whare in theme template not in content then you can use call the function $var = AuthUserAjax(); it will return the array. – Mitul Jul 2 '15 at 6:58
  • The function is not fired by a shortcode in fact. The user just has to click on the link "Log In" and a modal box appears. Then, when the user has filled up the form and submitted this one, the ajax request is fired and so the AuthUserAjax function. – Lancelot Jul 2 '15 at 7:02
  • Ok so instead of creating shortcode create wp-ajax action – Mitul Jul 2 '15 at 7:16

So, after hours of searching, I finally found where was my problem : another method in my class interfered with my ajax authentication system.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.