How can i get the current user in a custome middleware?

System Information
  • Strapi Version:4.0.7
  • Operating System: Windows
  • Database: SQLite
  • Node Version: 16.13.1
  • NPM Version:8.1.3
  • Yarn Version: 1.22.17

I’m trying to write a custom middleware for auditing log and I would like to know how can i get the current user who preforms the action.

Looking forward for the help and reply.

Assuming your middleware looks like this:

const mw = (context, next) => {
  ...
};

You can access a user (if one is authenticated) via context.state.user.