Extending User-Permissions in v5

I am migrating to v5, but it seems like my custom routes to extend user-permissions don’t work. I see them in Settings > Roles but when I click the gear Icon to see the path next to one of my routes, it doesn’t show anything and I get 404 when querying those new routes.

If someone can give me a simple working example of their strapi-server.ts I would be very grateful.

Otherwise, here is whati have (stripped down version). If anyone can point out why it isn’t working. FYI I did install the documentation plugin and the routes also don’t appear in the swagger page

module.exports = (plugin) => {
    plugin.controllers.user.getMe = async (ctx) => {

        const user = ctx.state.user;

        if (!user) {
            return ctx.unauthorized();
        }

        try {
            //Doing so manipulation

            return ctx.send(user);
        } catch (error) {
            return ctx.internalServerError({ message: "Failed to retrieve user", details: error });
        }
    };

    plugin.routes['content-api'].routes.push(
        {
            method: "GET",
            path: "/users/test/me",
            handler: "user.getMe"
        }
    )

    return plugin
}

I tried renaming the path to a bunch of things, to either overwrite the basic /api/users/me or to create a new different route and it doesn’t change anything. I do see getMe in my role permissions in the UI but that’s it, no route

This topic has been created from a Discord post (1288937679844802631) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

For the section that says Do something, are you trying to access an endpoint/collection that the user does not have permission to access? That may be the issue.

To get a list of the available controllers, you can just put a console log statement like this console.log(plugin.controllers.user) and see what is there.

woud like to know also how to extend user-permissions in v5

export default (plugin) => {
  // Capture the original factory function for the `auth` controller
  const originalAuthFactory = plugin.controllers.auth;

  // Create a new factory function that extends the original
  plugin.controllers.auth = ({ strapi }) => {
    const originalAuth = originalAuthFactory({ strapi });

    // Add a custom function to the `auth` controller
    originalAuth.customFunction = async (ctx) => {
      const someValue = ctx.request.body?.someValue;
      try {
        // Perform your logic here
        const result = await someAsyncOperation(someValue);
        ctx.response.body = result;
      } catch (error) {
        return ctx.badRequest("An error occurred", error.details);
      }
    };

    return originalAuth;
  };

  // Extend the plugin routes if needed
  plugin.routes['content-api'].routes.push(...customRoutes);

  return plugin;
};

this is how i’ve managed to add custom functions + routes to my strapi

since plugin.controllers.auth is an anonymous function

// node_modules/@strapi/plugin-users-permissions/server/controllers/auth.js:
module.exports(({strapi}) => { ... })

we have to do what i’ve shown in my code example in previous message to be able to add functions. and then adding a ts file for routes in a folder such as src/extensions/users-permissions/server/routes/index.ts doesn’t work. so we have to push it to the routes array of the plugin

<@674258272890519552> just to say one HUGE THANKS for this sharing.