• Hello, I installed and config JWT Authentication plugin and I could call both endpoints of:
    /wp-json/jwt-auth/v1/token | POST
    /wp-json/jwt-auth/v1/token/validate | POST
    I can get token from first endpoint and validate it by second endpoint but when I add that token to service calls WP return 401 error with “Sorry, you are not allowed to create new users.” message.
    I called “Add user” service with following URL, header and body:

    
    POST https://localhost/wordpress/wp-json/wp/v2/users
    Header: {
      "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3RcL3dvcmRwcmVzcyIsImlhdCI6MTU0NDAxMzIwOCwibmJmIjoxNTQ0MDEzMjA4LCJleHAiOjE1NDQ2MTgwMDgsImRhdGEiOnsidXNlciI6eyJpZCI6IjEifX19.1P_Pp26zaTQoz2DAiSolgJ52BfABIPoIY4pFGDMwHeM"
    }
    Body: {
      "username": "mytest",
      "email": "[email protected]",
      "password": "mytest"
    }
    

    response:

    
    {
    "code": "rest_cannot_create_user",
    "message": "Sorry, you are not allowed to create new users.",
    "data":{
    "status": 401
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Here is some typescript code you can wrangle whatever you need out of it….

    For this to work you need to install WP REST USER plugin to add a route to create users, WordPress API does not allow you to add users out of the box even if JWT plugin is installed

    This is code from my register page

    onSubmit(values) {
        let user_data = {
          username: values.username,
          name: values.fullname,
          email: values.username,
          password: values.password,
          phone: values.phone,
        };
        this.authService.doRegister(user_data)
          .subscribe(
            result => {
              console.log(result);
            },
            error => {
              console.log(error);
            }
          );

    In the authservice class

    doRegister(user_data){
        return this.http.post('your wordpress url here' + '/wp-json/wp/v2/users/register', user_data);
      }
    • This reply was modified 5 years, 9 months ago by Alekie. Reason: formatting

    Make sure that the user is EDITOR or ADMINISTRATOR in order to create users or post. A registered simple user can not create users or post.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘I get “Sorry, you are not allowed to create new users.” in service call by jwt’ is closed to new replies.