워드프레스 인증 + 난스 + Ajax + 템플리트 없음 - 쿠키 난스가 잘못되었습니다.
독자적인 템플릿을 작성할 때 난스를 사용하는 프로세스를 이해하고 있습니다.하지만 리액트를 개발하고 있습니다.JS App은 Wordpress REST API만을 사용하여 데이터를 풀하기 때문에 사용자는 index.php에 접속하지 않고 Ajax는 WP REST API를 호출합니다.
이제 난스 물건이 작동되지 않는다.지금까지 제가 한 일은 다음과 같습니다.
다음 엔드포인트를 추가했습니다.
register_rest_route('frontend', '/customer/', array(
    'methods' => 'GET',
    'callback' => 'get_customer'
));
register_rest_route('frontend', '/customer/', array(
    'methods' => 'POST',
    'callback' => 'create_user_and_login'
));
제 기능은 다음과 같습니다.
function get_customer()
{
    return get_current_user_id();
}
function create_user_and_login(){
    // dummy data for testing
    $credentials = ['user_login' => 'mail@mymail.de', 'user_password' => 'XXXX', 'remember' => true];
    // create a new user/customer via woocommerce
    $customerId = wc_create_new_customer($credentials['user_login'], $credentials['user_login'], $credentials['user_password']);
    if(is_a($customerId,'WP_Error')) {
        return $customerId;
    }
    // get the user & log in
    $user = get_user_by( 'id', $customerId );
    if( $user ) {
        wp_set_current_user( $customerId);
        wp_set_auth_cookie( $customerId );
    }
    // create new nonce and return it
    $my_nonce = wp_create_nonce('wp_rest');
    return $my_nonce;
}
POST를 실행하면/customer그 때문에create_user_and_login()새로 작성한 난스가 ajax 응답으로 반환됩니다.그 후 반환된 난스를 사용하여 다음 요청인 GET을 실행합니다./customer?_wpnonce=MY-NONCE단, 다음과 같은 오류가 표시됩니다.
{
    "code": "rest_cookie_invalid_nonce",
    "message": "Cookie nonce is invalid",
    "data": {
        "status": 403
    }
}
난스 문서를 확인했지만 내 문제에 대한 해결책을 찾을 수 없었다.세션이 동기화되지 않았을 수 있습니까?잘못된 세션에 난스가 생성되거나wp_set_auth_cookie그리고.wp_set_current_user올바르게 호출되지 않았습니까?아니면 기능을 사용해야 하나요?ReactJs와 Wordpress 백엔드를 분리하고 싶기 때문에 문제가 발생합니다.
POST 후에 쿠키 두 개를 받았는데wordpress쿠키와 awordpress_logged_incookie.
제가 무엇을 빠뜨리고 있나요?
당신이 전화했을 때$my_nonce = wp_create_nonce('wp_rest');nonce는 오래된 세션쿠키를 사용하여 작성됩니다.콜을 했을 때에도wp_set_auth_cookie그리고.wp_set_current_user그러나 다음 요청에서는 세션이 갱신됩니다.즉, 난스가 잘못되었음을 의미합니다.
답변과 같이 다음 후크(함수)를 추가합니다.예를 들어 php)를 사용하여 쿠키를 강제로 업데이트합니다.
        function my_update_cookie( $logged_in_cookie ){
            $_COOKIE[LOGGED_IN_COOKIE] = $logged_in_cookie;
        }
        add_action( 'set_logged_in_cookie', 'my_update_cookie' );
언급URL : https://stackoverflow.com/questions/46565162/wordpress-auth-nonces-ajax-no-templating-cookie-nonce-is-invalid
'sourcecode' 카테고리의 다른 글
| Angular.js ng-복수의 tr에 걸쳐 반복 (0) | 2023.02.11 | 
|---|---|
| 테두리 반지름을 1개의 코너에만 사용하는 방법(리액티브) (0) | 2023.02.11 | 
| Redux 저장소에 올바른 환원기가 없습니다. (0) | 2023.02.11 | 
| 각도 2 베타17: 속성 '맵'이 '관측 가능' 유형에 없습니다. (0) | 2023.02.11 | 
| 문서 본문 스크롤을 비활성화하려면 어떻게 해야 합니까? (0) | 2023.02.11 |