Support » Plugins » Hacks » Trouble with WordPress Ajax Login System – 302

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Lancelotkiin

    (@lancelotkiin)

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

    I am facing same problem, can you guide me through this?
    JS

    $('form#login').on('submit', function(e){
            $('form#login p.status').show().text(ajax_login_object.loadingmessage);
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: ajax_login_object.ajaxurl,
                data: {
                    'action': 'ajaxlogin', //calls wp_ajax_nopriv_ajaxlogin
                    'username': $('form#login #username').val(),
                    'password': $('form#login #password').val(),
                    'security': $('form#login #security').val() },
                success: function(data){
                    $('form#login p.status').text(data.message);
                    if (data.loggedin == true){
                        document.location.href = ajax_login_object.redirecturl;
                    }
                }
            });
            e.preventDefault();
        });

    myAction in functions.php

    function ajax_login(){
    
        // First check the nonce, if it fails the function will break
        $chk = check_ajax_referer( 'ajax-login-nonce', 'security' );
    
        // Nonce is checked, get the POST data and sign user on
        $info = array();
        $info['user_login'] = $_POST['username'];
        $info['user_password'] = $_POST['password'];
        $info['remember'] = true;
        $user_signon = wp_signon( $info, false );
        if ( is_wp_error($user_signon) ){
            echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.')));
        } else {
            echo json_encode(array('loggedin'=>true, 'message'=>__('Login successful, redirecting...')));
        }
    
        die();
    }

    Moderator bcworkz

    (@bcworkz)

    Hello MFaizyab_aimviz,

    There’s some gaps in the code you’ve presented. The ajax_login_object needs to be defined. This is usually done in PHP with wp_localize_script(). There should be a hidden field in the login form for the security nonce. The ajax_login() function needs to be added as a callback to the ‘wp_admin_nopriv_ajaxlogin’ action.

    Your JS should be enqueued by using wp_enqueue_script() where jquery is specified as a dependency. All WP jquery code should be wrapped in a noConflict wrapper:
    jQuery(document).ready(function($) { /*your code*/ });
    Call wp_enqueue_script() from within a ‘login_enqueue_scripts’ callback. The head section of the page with the login form should call do_action( 'login_enqueue_scripts' );

    A decent explanation of how AJAX works in general starts here:
    https://developer.wordpress.org/plugins/javascript/
    and runs on to a code summary page.

    FYI, tagging on to other people’s topics is discouraged in these forums. It’s much better to start a new topic, then your question is picked up by the “No Replies” filter that many of us use and many more people will see your question.

    If you need further guidance after considering my answers, I suggest you start a new topic and re-phrase your question to explain exactly what part you are not understanding.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Trouble with WordPress Ajax Login System – 302’ is closed to new replies.