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(…)”,
}