Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update user in Strapi

In Strapi every user-defined collection type has a default services which allow to create/find/update/etc. on the corresponding Model. For Example the below code in a Strapi controller will update a bill collection type with the given data:

await strapi.services.bill.update({id}, {verified: true, receipt_number})

But there is no service for User built-in collection type. I need to change the user's role by a custom controller.

like image 607
Mohammad Javad Khademian Avatar asked Jun 29 '20 05:06

Mohammad Javad Khademian


People also ask

How do you update Strapi components?

Take a look at the components section. You should always specify the ID of the component that you want to update. Otherwise, it will be deleted and recreated. So the ID of the new component will change.

Can Strapi be used as a backend?

Strapi is a headless CMS that allows you to easily build customizable backend services. You can integrate Strapi applications with any type of frontend and can deploy the application to the cloud. This tutorial guides you through the process of creating a simple React-native to-do app with Strapi as your backend.

What is new in Strapi v4?

Strapi Releases v4 of Headless CMS Strapi's headless posture also enables it to power front-end frameworks such as React, Gatsby, Vue. js, Next. js, and other static site generators. It also supports multiple databases as well as both REST and GraphQL APIs.


1 Answers

OK. I found the solution. We could modify or create a custom services and get inspired by this core services implementation here: https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services

for my case I have added this method to a custom service:

updateUserRole: async (user_id, role_id) => {
return await strapi.query('user', 'users-permissions').update({id: user_id}, {role: role_id});}

now I could access this service like this:

await strapi.services.customServiceName.updateUserRole(user_id, new_role)
like image 132
Mohammad Javad Khademian Avatar answered Sep 28 '22 01:09

Mohammad Javad Khademian