Error "Your 'nonce' value was incorrect. Use the 'get_nonce' API method." when trying to create user to Wordpress using Axios

323 Views Asked by At

I am trying to create a NextJS app using Wordpress as my backend. Part of my app is to register/create a user via Front-end(NextJS) using Axios. On my backend I am using JSON API and JSON API User plugins to try to create users. When I try to paste this in the browser url

http://nextjs-headless-wordpress.local/api/users/create_user/[email protected]&user_password=12345678&[email protected]&nonce=3b35836fcd&display_name=First%20User&u=admin&p=admin&insecure=cool`

it can succesfully create the user, but when I try to use this as the URL for my Axios call, I get the "Your 'nonce' value was incorrect. Use the 'get_nonce' API method." error. Below is my code and how I try to do it. What seems to be I am missing? Any help is very appreciated. Thank you.

axios.post( 'http://nextjs-headless-wordpress.local/wp-json/jwt-auth/v1/token',loginData)
.then( res => {
 if(res.data){
//get nonce
axios.get('http://nextjs-headless-wordpress.local/api/get_nonce/?controller=users&method=create_user')
.then( response => {

  console.log(response.data.nonce);
  // try to create user HERE
  let noncevalue = response.data.nonce;

  // generate auth cookie
  axios.get('http://nextjs-headless-wordpress.local/api/user/generate_auth_cookie/?insecure=cool&username=admin&password=admin')
  .then( respn => {
    console.log(respn);

    axios.post('http://nextjs-headless-wordpress.local/api/users/create_user/?user_login='+loginFields.email+'&user_password='+loginFields.password+'&user_email='+loginFields.email+'&nonce='+response.data.nonce+'&display_name='+loginFields.name+'&u=admin&p=admin&insecure=cool', {
      headers: {
        cookie: respn.data.cookie_name+'='+respn.data.cookie,
        "Access-Control-Allow-Origin": "*",
        'Access-Control-Allow-Headers': 'x-access-token',
        'Content-Type': 'application/json',
        'X-WP-Nonce': noncevalue
      },
      withCredentials: true
    })
    .then( res => {
      console.log(res);
    }).catch( err => {
      console.log('WP JSON API Create User Error: '+ err);
    })

  }).catch( erro => {
    console.log('Error'+erro)
  })


}).catch( error => {
  console.log('Get Nonce error: '+error);
})

} });

1

There are 1 best solutions below

0
Shawn M On

The plug-in you used hasn’t been updated in 8 years and according to the link it is deprecated. Look up Wordpress rest api. It’s endpoints are at “wp-json/wp/v2/“ so if you look at the full url “http://example.com/wp-json/wp/v2/“. I would try that way and investigate how to do what you’re trying to do with that means.