I wrote little plugin and I attempted to make use of Relaxation Api of WordPress for to speak admin web page to my relaxation. My downside is : failed rest_cookie_invalid_nonce, return 403 at all times.
My ajax name :
const form_information_user = jQuery('#form-information-client-login')[0];
let formData = new FormData(form_information_user);
formData.append('_wpnonce',obj_php.nonce)
jQuery.ajax({
sort: 'POST',
url : obj_php.ajax_url_login,
cache: false,
async: true,
processData: false,
contentType: false,
knowledge: formData,
success: operate( knowledge ) {
// Azioni da eseguire in caso di successo chiamata
console.log("success: ",knowledge);
},
error: operate( error ) {
// Azioni da eseguire in caso di errore chiamata
console.log("error: ",error);
}
});
My php file to point out view :
class Wp_Diet_Admin_Dashboard{
personal string $plugin_name;
personal string $model;
public operate __construct(string $plugin_name, string $model) {
$this->plugin_name = $plugin_name;
$this->model = $model;
$this->load_scripts();
$this->load_styles();
$this->set_scripts_variables();
$this->load_dashboard();
}
public operate load_dashboard(): void
{
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'views/wp-diet-admin-dashboard.php';
}
public operate load_scripts(): void
{
wp_enqueue_script( $this->plugin_name . '-just-validate.js', plugin_dir_url(dirname(__FILE__) ) . 'js/just-validate.js', array('jquery'), $this->model, true );
wp_enqueue_script( $this->plugin_name . '-dashboard.js', plugin_dir_url(dirname(__FILE__) ) . 'js/dashboard.js', array('jquery'), $this->model, true );
}
public operate set_scripts_variables() : void
{
$obj_php = [
"ajax_url_registration" => URL_API_V1 . 'form_information_user_registration',
"ajax_url_login" => URL_API_V1 . 'form_information_user_login',
"required" => __('Questo campo è obbligatorio','wp-diet'),
'nonce' => wp_create_nonce( 'wp_rest_dashboard_login' ),
"error" => __('Valore non valido per questo campo','wp-diet'),
"error_email" => __('Email non corretta','wp-diet'),
];
wp_localize_script($this->plugin_name . '-dashboard.js','obj_php',$obj_php);
}
}
My php file route
class Wp_Diet_Admin_Api{
personal string $plugin_name;
personal string $model;
personal WP_REST_Server $server;
public operate __construct(string $plugin_name, string $model) {
$this->plugin_name = $plugin_name;
$this->model = $model;
$this->server = new WP_REST_Server();
}
public operate load_routes( ): void
{
register_rest_route(
NAMESPACE_V1,
'form_information_user_login',
array(
'strategies' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'form_information_user_login'),
'permission_callback' => '__return_true',
)
);
}
public operate form_information_user_login(WP_REST_Request $request)
{
$physique = $request->get_body_params();
$e-mail = sanitize_email($physique['email']);
$go = sanitize_text_field($physique['password']);
$consumer = new GuzzleHttpClient();
$req = $client->request('POST', 'http://www.localhost:3000/registration-success', [
'form_params' => ['email' => $email, 'password' => $pass]
]);
//jwt-licenza / messaggio d'errore
$response = json_decode($req->getBody()->getContents(), true);
// wp_send_json('okay');
print_r(wp_get_all_sessions());
}
}
If I not use nonce argument in physique request it work however I am unable to know if the consumer is logged in, if the request is authorized, and I am unable to handle a session for saving a jwt.
I wish to acknowledge who’s the consumer who sends me the request and whether it is attainable to insert the saving of a jwt with relative refresh
Any recommendation is welcome, thanks for assist
Finest regalds,