django-rest-frameworkdjoser

Token destroy with djoser


I'am using django and trying to destroy a token when user log out, this is the function I'am using :

    const logMeOut = async () => {
        setAnchorEl(null);
        const response = await fetch('http://127.0.0.1:8000/api-auth/token/logout/', {
            method: 'POST',
            headers: {
                'Authorization': 'Token '.concat(GlobalState.userToken),
                'Content-Type': 'application/json',
            },
        })};

However my server is throwing :

POST http://127.0.0.1:8000/api-auth/token/logout/ 401 (Unauthorized)

What I'am I doing wrong ? I know it's something about the headers or something like that... but I have been switching headers and nothing changes.


Solution

  • Okay, I found out what the problem was and I'll leave it here for anyone who might be facing the same issue.

    In my particular case, I was getting unauthorized because I send the same token multiple times and the token was destroyed the first time I sent the request to the backend...

    Basically, I didn't destroy the token in the frontend and kept sending the same token which was already destroyed, which means there is nothing wrong with the piece of code above and it should work.