[details=“System Information”]
- Strapi: 4.14.0:
- On: Mac OS:
- db: postgree:
- node: 18.15.0:
- npm: 9.5.0:
- yarn: 1.22.19:
My Routes Middleware does not get called when a user try to update, or delete a post, this is my middleware.
/**
* `isOwner` middleware
*/
import { EntityService, Strapi } from '@strapi/strapi';
const STRAPI_CODE_SUPERADMIN = "strapi-super-admin";
export default (config, { strapi }: { strapi: Strapi }) => {
return async (ctx, next) => {
console.log("isOwner middleware");
const user = ctx.state.user as EntityService.GetValues<"admin::user">;
if(user.roles.find((role) => role.name === STRAPI_CODE_SUPERADMIN)) {
console.log("User is Admin, skipping isOwner middleware");
return next();
}
const entryId = ctx.params.id ? ctx.params.id : undefined;
let entry = {} as EntityService.GetValues<"api::post.post">
/**
* Gets all information about a given entry,
* populating every relations to ensure
* the response includes author-related information
*/
if (entryId) {
entry = await strapi.entityService.findOne(
"api::post.post",
entryId,
{ populate: "*" }
);
}
console.log("entry", entry.author);
/**
* Compares user id and entry author id
* to decide whether the request can be fulfilled
* by going forward in the Strapi backend server
*/
if (user.id !== entry.createdBy.id || user.id !== entry.updatedBy.id) {
return ctx.unauthorized("This action is unauthorized. Only Post Authors can Edit their Posts.");
} else {
return next();
}
};
};
And my route
/**
* post router
*/
import { factories } from "@strapi/strapi";
export default factories.createCoreRouter("api::post.post", {
config: {
update: {
middlewares: ["api::post.is-owner"],
},
delete: {
middlewares: ["api::post.is-owner"],
},
},
});
If i run yarn strapi middlewares:list
is see the middleware registred as “api::post.is-owner”,
Thanks for your help