0

I have a custom Strapi plugin with the following route:

module.exports = [
  {
    method: "GET",
    path: "/export/:formToExport/:deleteAfterExport",
    handler: "myController.index",
    config: {
      policies: [],
    },
  },
];

And I have an admin page with a button that calls the following function to call that endpoint:

  const handleDownload = async () => {
    try {
      const url = `/export-form-submissions/export/${encodeURIComponent(
        formToExport
      )}/${deleteAfterExport}`;
      const response = await fetch(url);

      //do more stuff
  };

I get a 401 error when I call this function unless I disable the authentication with auth: false in the route's config. I don't want to disable the authentication check as I only want logged in users to be able to make the request.

Is there a way I can include the login session information in the request so that I can call the endpoint from the plugin's admin page?

Chris Lau
  • 77
  • 1
  • 6

1 Answers1

0

Hi you have to use @strapi/helper-plugin

There is special hook, to get you admin auth:

import { useFetchClient } from '@strapi/helper-plugin';

… () => {

  const { get, post } = useFetchClient();

}

Ref: https://github.com/strapi/strapi/blob/main/packages/core/helper-plugin/src/utils/getFetchClient/index.js

antokhio
  • 1,497
  • 2
  • 11
  • 16