Creating middleware / policy

System Information
  • Strapi Version: 4.20.0
  • Operating System: Docker
  • Database: PostGIS
  • Node Version: v20.10.0
  • NPM Version: 10.2.4
  • Yarn Version: N/A

Hi folks!

I’m trying to create a middleware and a policy for the user-permissions plugins. But I’m having problems checking whether my code is working or not. I created a middleware and a policy using the strapi CLI, but I won’t be able to make my endpoints reach them.

The policy.

I tried to create a “test-policy” policy. I run the CLI:

? Strapi Generators policy - Generate a policy for an API
? Policy name test-policy
? Where do you want to add this policy? Add policy to an existing plugin
? Which plugin is this for? users-permissions

But I think I need to register this policy, right? But where? Because this policy is not showing up when I run npm run strapi policies:list. It just shows when I generate a policy, selecting the “Add policy to root of project” option.

The CLI created the file src/plugins/users-permissions/server/policies/test-policy.ts.
But how can I implement this in my routes?
I found a tutorial saying to create the file src/extensions/users-permissions/strapi-server.js, another one saying to create the file src/plugins/users-permissions/server.js.
What is the correct directory?

I have the same questions about the middleware, I run the CLI and they created this file:
But this doesn’t show up when I run npm run strapi middlewares:list.

My questions are more related to how I implement middlewares and policies than what I want to do with them.

These are the links that I checked trying to implement middleware and policies on users-permissions: