Wordpress Auth + Nonces + Ajax + 无模板 - Cookie nonce 无效
Wordpress Auth + Nonces + Ajax + No Templating - Cookie nonce is invalid
我了解在创建自己的模板时使用随机数的过程。
但是我正在开发一个仅使用 Wordpress REST API 来提取数据的 ReactJS 应用程序,因此用户永远不会访问 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
}
}
我检查了the nonce documentation but I could not find a solution for my problem. Could it be that the sessions are out of sync? So that the nonce is created on the wrong session or wp_set_auth_cookie
and wp_set_current_user
are not called correctly? Or do I have to use the wp_localize_script
函数?这会出现问题,因为我想将 ReactJS 和 Wordpress 后端分开。
我在 POST 之后得到了两个 cookie,一个 wordpress
cookie 和一个 wordpress_logged_in
cookie。
我错过了什么?
似乎当您调用 $my_nonce = wp_create_nonce('wp_rest');
时,随机数是使用旧会话 cookie 创建的,即使您调用 wp_set_auth_cookie
和 wp_set_current_user
也是如此。但是在下一个请求中会话被更新,这意味着随机数是错误的。
如答案所示,添加以下挂钩(例如functions.php)以强制更新 cookie:
function my_update_cookie( $logged_in_cookie ){
$_COOKIE[LOGGED_IN_COOKIE] = $logged_in_cookie;
}
add_action( 'set_logged_in_cookie', 'my_update_cookie' );
我了解在创建自己的模板时使用随机数的过程。 但是我正在开发一个仅使用 Wordpress REST API 来提取数据的 ReactJS 应用程序,因此用户永远不会访问 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
}
}
我检查了the nonce documentation but I could not find a solution for my problem. Could it be that the sessions are out of sync? So that the nonce is created on the wrong session or wp_set_auth_cookie
and wp_set_current_user
are not called correctly? Or do I have to use the wp_localize_script
函数?这会出现问题,因为我想将 ReactJS 和 Wordpress 后端分开。
我在 POST 之后得到了两个 cookie,一个 wordpress
cookie 和一个 wordpress_logged_in
cookie。
我错过了什么?
似乎当您调用 $my_nonce = wp_create_nonce('wp_rest');
时,随机数是使用旧会话 cookie 创建的,即使您调用 wp_set_auth_cookie
和 wp_set_current_user
也是如此。但是在下一个请求中会话被更新,这意味着随机数是错误的。
如答案所示,添加以下挂钩(例如functions.php)以强制更新 cookie:
function my_update_cookie( $logged_in_cookie ){
$_COOKIE[LOGGED_IN_COOKIE] = $logged_in_cookie;
}
add_action( 'set_logged_in_cookie', 'my_update_cookie' );