Can I tell in the beforeUpdate hook if the update comes from Admin

System Information
  • Strapi Version: 4.13.6
  • Operating System: MacOS
  • Database: Postgres
  • Node Version: 18.18.0
  • NPM Version: 9.8.1
  • Yarn Version: 1.22.19

Does anyone know if there’s a way to determine from the event object within the beforeUpdate or beforeCreate hook whether the update is coming from the Admin UI?

Looking at the object I can probably assume it’s the admin UI if the populate object looks like this:

    "populate": {
      "channel_items": { "count": true },
      "tax_rate": { "count": true },
      "createdBy": true,
      "updatedBy": true
    },

However, this feels very flaky and subject to change to me.

(For context, I want to set an update_source field on my model and I can do this manually for any API requests or a couple of queue workers I have in my project, but want to set it automatically when coming from the Admin UI rather than rely on users to set it.)

For the admin you have to be authenticated if you get the full ctx with
const ctx = strapi.requestContext.get();
you can check ctx.state.auth.strategy.name to see what type of authentication is being used.

1 Like

Brilliant, thanks, that’s just what I need.

1 Like