Create a new, using api?

How can i create a new user using the strapi api
I have a personal table and this is related to the “one to one” user
how can i change the contrller to register the person and at the same time create the login and password?

Why don’t you add all the necessary details/fields by using extensions to the user-permissions plugin?

You should use lifecycles and extensions for this.

Create an extension under ./extensions/users-permissions/models/User.js

Now take a look at the below code:

'use strict';
module.exports = {
   lifecycles: {
    async afterCreate(data, model) {
       //id of the newly created user.
       let userId = data.id; 
       //creating the profile with relation to that user.
       await strapi.services.profile.create({
         ...DataForProfile,
         users_permissions_user: userId
       });
    },
  },
};

Thanks but…
I have one that sends data to the api.
login, password, name, gender and email

this will all be received on the “/ people” route
already made the modification of the route and the file controllers / people.js

are not sure how to register the user “login & password” and then get the id and save people’s data “name, sex and email”
with this code that you passed, I don’t know how it will work …
thanks

On your people controller, you can use the user-permissions's add service.

Example how to create an user by using service:

let user = await strapi.plugins['users-permissions'].services.user.add({
    blocked: false,
    confirmed: true, 
    username: 'new_username',
    email: 'test@testemail.com',
    password: 'secretpassword', //will be hashed automatically
    provider: 'local', //provider
    created_by: 1, //user admin id
    updated_by: 1, //user admin id
    role: 1 //role id
});

Where the user will contain the created id. You can use that id to create the people entity:

await strapi.services.people.create({
   //people data here
   // ...
   users_permissions_user: user.id //map relation with the user
});
1 Like

Thanks, you can return the data and join the JWT??

Note:
I didn’t find it in the documentation

What do you mean? What data? Of the create people or user?

I know the data is in the variable user.
Only comes the user creation data, how to bring also the JWT?
Example:
return data:
{
id": 9,
“username”: “teste”,
“email”: “xx@testemail.com”,
“provider”: “local”,
"jwt": “eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVC(…)”,
}

In this request, you used

await strapi.plugins['users-permissions'].services.user.add({

What do you use for V4.5.3? I can’t create a user through my controller.

I am trying to create a user through the entity service but it does not allow me to validate even though I can create them.

const password = await strapi.service("admin::auth").hashPassword("testtest");

        

console.log(password);

      let newUser = await strapi.entityService.create(

        "plugin::users-permissions.user",

        {

          data: {

            username: Email,

            email: Email,

            password: password,

            confirmed: true,

            OrganizationName: OrganizationName,

            OrganizationId: organizationId,

            TypeOfRegistration: "Admin",

            role: userPerm.id,

            FullName: Name,

            UserId: uuidv4(),

          },

        }

      );

The answer to this is documented here and I can confirmed I have managed to create user and login user :