Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

595
Views
Strapi v4: How to access context within a service

When I create a custom controller in Strapi I get convenient access to a Context object where I can get the current user and use that user's data as I wish:

module.exports = createCoreController("api::event.event", ({ strapi }) => ({
  //Get logged in  users
  async me(ctx) {
    const user = ctx.state.user;

    if (!user) {
      return ctx.badRequest(null, [
        { messages: [{ id: "No authorization header was found" }] },
      ]);
    }
    const data = await strapi.entityService.findMany("api::event.event", {
      filters: {
        user: user.id,
      },
    });

    if (!data) {
      return ctx.notFound();
    }

    const sanitizedEntity = await sanitize.contentAPI.output(data);

    return { data: sanitizedEntity };
  },
}));

However when I create a custom service by trying to extend the Core services, I don't seem to have the context object as above:

module.exports = createCoreService("api::event.event", ({ strapi }) => ({
  //https://docs.strapi.io/developer-docs/latest/development/backend-customization/services.html#extending-core-services

  async create(params) {
    console.log("inside event.js - create");
    console.log("params", params);

    console.log("params to save", params);

    // some logic here
    const result = await super.create(params);
    // some more logic

    return result;
  },

  async update(entityId, params) {
    params.data.user = entityId;

    // some logic here
    const result = await super.update(entityId, params);
    // some more logic

    return result;
  },
}));

If possible I would like to access the context object because I would like to access the user info and get user id and add that id as the owner or creator of that entry.

Is it possible and how does one do that???

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!