CreatedBy missing when submitted trough API

Hi!

I want to create some audit logs using the afterCreate lifecycle but I am missing the createdBy in the event.result.
Even when I create an entry trough the API using an Admin role frontend user’s bearer token, the createdBy in the strapi admin panel is empty.

Why is this?

If I’m correct, the only way I can tackle the audit log issue now (need to track WHO did it) is by using middleware as the middleware has the request info with the user.

Is this all correct or am I missing something?

f you’re looking to implement audit logs, using middleware is a reasonable approach. Middleware can intercept requests and responses, allowing you to perform actions such as logging information about the request, including the user making the request.
Here is your solution and coding:
// ./middlewares/audit-log.js
module.exports = async (ctx, next) => {
// Log audit information here, e.g., ctx.state.user
console.log(“User making the request:”, ctx.state.user);

// Continue with the request lifecycle
await next();
};
// ./config/middleware.js
module.exports = {
settings: {
// Other settings…
},
load: {
// Other middleware…
order: [
// Other middleware…
‘audit-log’, // Add this line to include your audit-log middleware
],
},
};

1 Like

Thanks @Richard_Goodwinn, I guess the middleware solution it is then!

1 Like

My pleasure

Hi!

This does not work for ‘content created’ audit logs, as the middleware has no information about the to-be-created content type’s id. I guess this is unfixable?

I would be curious as to why createdBy is missing from the lifecycles, no Strapi dev gave any info on this?