How do I get my JWT token inside my custom plugin?

I have created a plugin which will call the API, to import some data:

  async handleSubmit(event) {

    const json = JSON.parse(this.state.value);
    var result = await`${apiUrl}/epgp-importer/importEPGP`, json);
    console.log('end result was... ', result);

However, this fails because this is a non-authenticated request, despite it being called from a plugin.
I need to add the JWT to the headers, but I can’t figure out how to get the users JWT from within my plugins front end.

Does anyone know how?

Thank you.

Anyone know the answer this this?

Surely this must be a common use case, a plugin that is accessed via admin screen should be able to access privileged endpoints.

No one knows how?

Here is a quick and dirty way you can get it.

const getToken = () => {
    return `Bearer ${JSON.parse(window.sessionStorage.jwtToken)}`;

You can get it straight from the browser.

In version 3, you may use “request” from

import {request} from "strapi-helper-plugin";

which get the jwt token automatically into the header.

You may get more detail from How to create your own plugin on strapi (1/4)

Apparently, you can get it through import {auth} from “@strapi/helper-plugin” too.
I believe this is the authentication of your strapi account instead of the super admin/admin that have access to the content type collection data.

I tried to use the JWT token (the way you show) to access my API but it gave me a request fail.

and to @georgezhang, the request is deprecated in V4 too.

[Strapi v4]