How to add custom routes in users-permissions strapi

Viewed 20

I want to add custom routes in users-permissions controller for creating different setting for different user for example data of user, location of user for that I have to create a default controller and default route for that I have written following code in following directories. In theh extension/users-permissions/controllers/users-permissions.js

const { sanitizeEntity } = require('strapi-utils')
const sanitizeEntity = (user) =>
  sanitizeEntity(user, {
    model: strapi.query('user', 'user-permission').model,
  })

module.exports = {
  setSettings: async (ctx) => {
    const { id, Contactinfo, locations } = ctx.state.user
    const { details, detailSlot, location, locationSlot } = ctx.request.body
    let newInfo = [...Contactinfo]
    let newLocations = [...locations]
    if (typeof details !== 'undefined' && typeof locationSlot !== 'undefined') {
      newInfo[detailSlot] = details
    }
    if (typeof location !== 'undefined' && typeof detailSlot !== 'undefined') {
      newLocations[locationSlot] = location
    }
    let newUser = strapi.plugin['users-permissions'].services.user.edit(
      { id },
      { Contactinfo: newInfo, locations: newLocations },
    )
    newUser = sanitizeUser(newUser)
    ctx.send(newUser, 200)
  },
}

In the extension/users-permissions/routes/routes.js

module.exports = {
  routes: [
    {
      method: 'POST',
      path: '/set-settings',
      handler: 'User.setSettings',
      config: {
        policies: [],
      },
    },
  ],
}

It does not showing me any error but meanwhile it does not displaying another routes in users-permissions in Strapi UI also when I posted the request in postman, it shows method not allowed.

If anyone Knows about it and can able to figure out what's the problem will be very helpful.

0 Answers
Related