mercredi 3 février 2016

Wordpress cookies not saved to browser in ajax response

We use WP REST API v2, we created a function that authenticates user and if user authentication succeeds, we then return cookies to be saved in browser, so the user can enter the WP Admin Dashboard as logged user. But, all this happens from an external AJAX request, outside the WP Admin dashboard

function authUser( WP_REST_Request $request )
{
    // Get login information
    $username   = $request->get_param( 'username' );
    $password   = $request->get_param( 'password' );

    $user = wp_authenticate( $username, $password );

    if ( is_wp_error( $user ) ) {
     return false;
    }

    $converter = new Encryption;
    $encoded = $converter->encode($username . ":" . $password);

    // Set Cookie: NOT SAVED TO BROWSER!!!
    wp_set_auth_cookie($user->ID, true);

    return ['key' => $encoded, 'cookie' => $_COOKIE];
}

The wordpress auth cookies are perfectly returned, but they AJAX response does NOT seem to save them to browser. Ideas?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire