[V4] How do i extend the user object in Strapi context?

I have added a relationship to my user model and i would like that to be available within the authenticated user object context as mentioned here

I read somewhere that currently relationships dont get added to this by default, to keep the size of the user object to a minimum.

How would i add this manually?

There are a few different ways. As long as you can mutate the ctx, it should be possible.

  1. I imagine you can write a custom middleware to fetch the user and set ctx.state.user to the new value (I have not tested this). This is probably easiest way but is inefficient in that you have to fetch the user again.
  2. In Strapi version 3, you could do the same as the above within a policy. I would imagine this would work, but I have not tested in version 4.
  3. You can edit the strapi code to fetch more relations and save this change using patch-package. This is what I am doing. The relevant function is here. There is obviously some risk in modifying internal Strapi code though.
    4.I am using graphql and have added the nexus-shield plugin to protect individual queries/mutations. You can mutate the ctx here too.
1 Like