Skip to content

Instantly share code, notes, and snippets.

@n2o
Created May 4, 2023 13:50
Show Gist options
  • Save n2o/3c2cb2f801b810076703cdd6b710b169 to your computer and use it in GitHub Desktop.
Save n2o/3c2cb2f801b810076703cdd6b710b169 to your computer and use it in GitHub Desktop.
Strapi: The missing Docs

Strapi: The missing Docs

Strapi is a great project, but lacks of a good documentation concerning customization. This gist presents some of my findings / experiments to customize strapi.

users-permissions plugin

Add policies to existing routes

My use-case: find and findOne need to be active to keep nested queries on other user-relations. But if I allow authenticated users to access these routes, they can access all users. So I customize these routes with my own policies:

// ./src/extensions/users-permissions/strapi-server.ts

export default (plugin) => {
  plugin.routes["content-api"].routes.forEach((route) => {
    if (route.handler === "user.findOne") {
      route.config.policies = ["global::my-policy"]
    }
    if (route.handler === "user.find") {
      route.config.policies = ["global::my-policy"]
    }
  })

  return plugin
}

Add own services

In my use-case, I want to add more services directly to the users without having to create a new plugin. This is possible by extending the user service:

// ./src/extensions/users-permissions/strapi-server.ts

function myService() {
  // do something
}

export default (plugin) => {
  const oldServices = plugin.services.user
  plugin.services.user = ({ strapi }) => {
    return {
      ...oldServices({ strapi }),
      myService,
    }
  }
  return plugin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment