How create custom endpoint for Users in strapi v4
Yeah! It necessary for me to. Same logic as v3 doesn’t work!
I attached few screens. What i doing wrong?
Hey andrew,
To extend a plugin’s interface using the ./src/extensions
folder in v4 of strapi:
- Create the
./src/extensions
folder at the root of the app, if the folder does not already exist. - Create a subfolder with the same name as the plugin to be extended, in your case
users-permissions
. - Depending on what needs to be extended:
- create a
strapi-server.js
file to extend a plugin’s back end using the Server API - or create a
strapi-admin.js
file to extend the admin panel.
Since you are looking for controllers and routes, here is an example of a backend extension:
// path: ./src/extensions/users-permissions/strapi-server.js
module.exports = (plugin) => {
plugin.controllers.user.find = (ctx) => {
console.log('test');
};
return plugin;
};
This guide is from: Plugins extension - Strapi Developer Docs
Merci beaucoup j’ai tester sa marché
Yeah! Thank you so much! That works like i wanted))
This screenshot for those who was like me )))
Can you answer one more question please, or give me a link. I dont understand about router types (admin or content-api). For what things those types?
Hey @Andrew_Andrew, I’m glad that helped!
Routes are related to your api.
You could have routes for your admin panel, or for the client side API (content-api)
For example, imagine you are developing a custom plugin (or extending one), and you need access to some data in the frontend part of your plugin but ONLY for the admin side. You will create a route with the type of admin-api and you can now make an API call to that custom route/controller and get data that you need.
As expected, the route you created (let’s say /plugin-name/templates), will be added to the settings and you could restrict it by admin roles:
However, you may also need to create controler and a route for the client side with the type content-api, which would allow you to access it like your other content type API’s (/api/restaurant etc…), and restrict it by user role types and as they show up in this section of the settings:
I hope that helps!
All the best
Hi, I followed the above instructions, but when I make the strapi-server.js file, I get strapiServer is not a function error and the server crashes. Any Idea what I’m doing wrong?
You have to return something, like this:
// strapi-server.js
module.exports = (plugin) => {
plugin.controllers.user.find = (ctx) => {
console.log('test');
};
return plugin;
};
Thank you so much. Very helpful. A lot of new changes going from Strapi 3 to 4. Glad we have this forum.
Thank you all. Great thread.
I created a custom endpoint for users but don’t know how to access “Entity Service API”.
How can I access strapi
? and update the user like so:
const entry = await strapi.entityService.findOne('api::user.user', 1, {
populate: { someRelation: true },
});
Just found out a way! Here is my solution for those curious.
Looks like strapi
is global and can be access anywhere.
const entity = await strapi.entityService.update(
"plugin::users-permissions.user",
ctx.state.user.id,
{ data }
);