Get user identity inside lifeCycle hook

System Information
  • Strapi Version: 3.6.8:
  • Operating System: Windows 11:
  • Database: mysql:
  • Node Version: 14.18.1:
  • Yarn Version: 1.22.11:

Hello guys, I face a little issue, and this is just one of the thing, there is just need to be a more elegant way. I build after update lifecycle hook, to log the update and this I need the identity of the user, who did the update.
But I am unable to do that, right now I am doing really nasty “hack” so I wrote a middleware, that gives me the jwc token, and in the lifehook I make a request to /user/me, to get the full identity. How can i do this better?

Hi, not sure if you still face this issue but this is what I use.

export default ({ strapi }: { strapi }) => {
  strapi.db.lifecycles.subscribe((<Subscriber>{
    async afterFindOne(event): Promise<void> {
      // Get the request config
      const ctx = strapi.requestContext.get();

      console.log(ctx?.state?.user);
    },
  }) as Subscriber);
}

Documentation can be found here: Requests and Responses - Strapi Developer Docs

4 Likes