I am using Strapi v5.0.6
I have the following middleware code in the global space:
module.exports = (config, { strapi }) => {
return async (ctx, next) => {
const entryId = ctx.params.id;
const user = ctx.state.user;
const userId = user?.id;
console.log('entryId', entryId);
console.log('user', user);
console.log('userId', userId);
if (!userId) return ctx.unauthorized(`You can't access this entry`);
const apiName = ctx.state.route.info.apiName;
function generateUID(apiName) {
const apiUid = `api::${apiName}.${apiName}`;
return apiUid;
}
const appUid = generateUID(apiName);
if (entryId) {
const entry = await strapi.entityService.findOne(appUid, entryId, {
populate: "*",
});
if (entry && entry.user.id !== userId)
return ctx.unauthorized(`You can't access this entry`);
}
if (!entryId) {
ctx.query = {
...ctx.query,
filters: { ...ctx.query.filters, user: userId },
};
}
await next();
};
};
The code works for findOne but gives me the following error for find
{
"data": null,
"error": {
"status": 400,
"name": "ValidationError",
"message": "Invalid key user",
"details": {
"key": "user",
"path": "user",
"source": "query",
"param": "filters"
}
}
}
The schema for the user is
"user": {
"type": "relation",
"relation": "manyToOne",
"target": "plugin::users-permissions.user",
"inversedBy": "quizzes"
}
Any help would be gratefully accepted and if i have any hair left after pulling it out all day would be a bonus!