I'm trying to build an application using ReactJS and Wordpress Rest Api. I'm using JWT Auth Plugin and it works great but if I try to create a new user without being logged I get this error below:
{
"success": false,
"statusCode": 403,
"code": "jwt_auth_no_auth_header",
"message": "Authorization header not found.",
"data": []
}
I understand that I need a token to make POST requests but if it's a new user that is trying to register on my application, how can I generate a token for a user that doesn't exist yet?
Here is the code of my user_post.php:
<?php
function api_user_post($request){
$email = sanitize_email($request['email']);
$senha = $request['senha'];
$nome = sanitize_text_field($request['nome']);
$user_exists = username_exists($email);
$email_exists = email_exists($email);
if(!$user_exists && !$email_exists && $email && $senha) {
$user_id = wp_create_user($email, $senha, $email);
$response = array(
'ID' => $user_id,
'display_name' => $nome,
'first_name' => $nome,
'role' => 'professor'
);
wp_update_user($response);
}else{
$response = new WP_Error('email', 'Email já cadastrado.', array('status' => 403) );
}
return rest_ensure_response($response);
}
function resgister_api_user_post(){
register_rest_route('api', '/usuario', array(
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'api_user_post',
),
));
}
add_action('rest_api_init', 'resgister_api_user_post');
?>