useFetchClient API Calls Returns 401 and Logs out Admin

I’m building a plugin for the Admin Panel and trying to make an API call from my React component to my Strapi API.

I’m using the useFetchClient module to make an API request when a button is clicked, like so:

import React, { useEffect } from 'react';
import { Button } from '@strapi/design-system';
import { useFetchClient } from '@strapi/helper-plugin';

const SyncAction = () => {
    const { put } = useFetchClient()

    const syncPush = async () => {
        try {
            const response = await put(`/api/cms-sync/push/1`)
            console.log(response)
        } catch (error) {
            debugger
            console.log(error)
        }
    }

    return (
        <Button onClick={syncPush} variant='success-light'>
              Sync Push
        </Button>
    );
};

export default SyncAction;

Whenever the button is clicked, a 401 error code is returned and the logged in Admin is redirected to the Strapi Admin login page (session terminated).

Based on all the code samples I see, this should work. I’m logged in as a Super Admin user. I can also confirm that the same Bearer Token being used for my active session is being submitted as an authorization header with the request.

Does anyone know what may be happening here?

3 Likes

I got the same issue, did you find a way to solve it? I’m completely desperate

I got exactly the same problem. Is there already a way to solve it?

I ran into the same issue, I was able to solve it by not including ‘api/’ in my request url. Still unclear on why that worked, but might be worth a shot.