Forbidden: /api/v1/token/logout/ "POST /api/v1/token/logout/ HTTP/1.1" 403 58

132 views Asked by At

I am experiencing this problem following a tutorial and I can't identify the error in my "MyAccountView.vue" page. I tried changing to re_path and it did not work.

Forbidden: /api/v1/token/logout/
[16/Oct/2023 19:01:35] "POST /api/v1/token/logout/ HTTP/1.1" 403 58

CODE:

URLS.PY

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/v1/', include('djoser.urls')),
    path('api/v1/', include('djoser.urls.authtoken'))
]

SETTING.PY

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSESS':(
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSESS':(
        'rest_framework.permissions.IsAuthenticated',
    )
}

ERROR IMAGES

Getting this error in browser's console

MyAccountView.vue

If it works i'm supposed to forward on "/" or home page of my site.

methods: {
        logout() {
            axios
                .post("/api/v1/token/logout/")
                .then(response => {
                    axios.defaults.headers.common["Authorization"] = ""

                    localStorage.removeItem("token")

                    this.$store.commit('removeToken')

                    this.$router.push('/')
                })
                .catch(error => {
                    if (error.response) {
                        console.log(JSON.stringify(error.response.data))
                    } else if (error.message) {
                        console.log(JSON.stringify(error.message))
                    } else {
                        console.log(JSON.stringify(error))
                    }
                })
        }
    }
1

There are 1 answers

3
Arun T On

Looks like you are using djoser

You need to pass the token while calling the logout endpoint in your Post request

Example:

.post('api/v1/token/logout/', token,
   {
     headers: {
       Authorization: `Token ${token}`  --> Add your token here.
     }
   })