authenticationpluginsstrapi

Custom plugin routes are not accessible using API tokens


I am using strapi V4. I have created the custom plugin where I have defined some routes in the src/plugins/{plugin_id}/server/routes/index.js

I want to authenticate these endpoints with the API token but the problem is that when I generate the token, those routes are not listed out there which fails the authentication on those routes with the API token.

Any idea on how can I solve this?

NOTE: those endpoints are accessible when set auth:false which indicates that plugin configuration is correct.


Solution

  • There are two different route types (admin and content-api). API tokens and U&P only work on content-api routes, not admin.

    You can create content-api routes from your custom plugin with the following syntax:

    const contentAPIRoutes = require('./content-api');
    
    const routes = {
      'content-api': {
        type: 'content-api',
        routes: contentAPIRoutes,
      },
    };
    
    module.exports = routes;
    

    Reference:
    https://github.com/strapi-community/plugin-healthcheck/blob/b86874915cf2087dd84f4fa922476f7071791e6d/server/src/routes/index.js#L4