自分で作成したテンプレートを作成するときにノンスを使用するプロセスを理解しています。 しかし、データをプルするためにWordpress REST APIのみを使用するReactJS Appを開発しています。そのため、ユーザーはindex.phpにアクセスすることはありませんが、WP Rest APIへのAjaxコールはありません。Wordpress Auth + Nonces + Ajax + No Templating - クッキーノンスが無効
今、ノンスの仕事はできません。
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' => '[email protected]', '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;
}
私は今create_user_and_login()
をトリガー/customer
にPOSTを実行する場合
/customer?_wpnonce=MY-NONCE
にGETを実行するために、返されたnonceを使用しますが、私はエラーを取得:
{
"code": "rest_cookie_invalid_nonce",
"message": "Cookie nonce is invalid",
"data": {
"status": 403
}
}
私はthe nonce documentationをチェックするが、私は私の問題の解決策を見つけることができませんでした。セッションが同期していない可能性がありますか? Nonceが間違ったセッションで作成されるか、wp_set_auth_cookie
とwp_set_current_user
が正しく呼び出されないようにするには?またはwp_localize_script
機能を使用する必要がありますか?私はReactJSとWordpressバックエンドを分離したいので、これは問題になるでしょう。
POSTの後に2つのCookieがあり、wordpress
のCookieとwordpress_logged_in
のCookieがあります。
私には何が欠けていますか?
おかげでたくさん!それは動作します。 – Dario