I am using django restframework with TokenAuthentication. So if I go for example to this api call:
http://192.168.1.67:8000/api/categories/
Then the user has to login. But when I trigger the logout button. the logout doens't work. And I see in the terminal. That there are two api calls are triggered when the user triggers the logout button:
[18/May/2023 10:42:14] "GET /api-auth/logout/?next=/api/categories/ HTTP/1.1" 302 0
[18/May/2023 10:42:15] "GET /api/categories/ HTTP/1.1" 200 32866
So this is the urls.py:
urlpatterns = [
path('create/', views.CreateUserView.as_view(), name='create'),
path('logout/',views.LogoutView.as_view(), name='logout'),
path('token/', views.CreateTokenView.as_view(), name='token'),
path('me/', views. ManageUserView.as_view(), name='me'),
path('login/', obtain_auth_token, name='login' )
]
and settings.py:
LOGOUT_REDIRECT_URL = "/"
ACCOUNT_LOGOUT_REDIRECT_URL = "/"
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
'DEFAULT_SCHEMA_CLASS':'drf_spectacular.openapi.AutoSchema',
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'SWAGGER_SETTINGS' : {
'LOGIN_URL': 'rest_framework:login',
'LOGOUT_URL': 'rest_framework:logout'
}
}
and views.py:
class AnimalViewSet(viewsets.ModelViewSet):
"""
This API endpoint allows viewing animal descriptions.
- To view a specific animal, append the url with its [/id](/api/animal/1/).
- To view all the animals in a category, use [/category/id](/api/categories/1/).
"""
queryset = Animal.objects.all().order_by('name')
serializer_class = AnimalSerializer
permission_classes = ( IsAuthenticated, )
class CategoryViewSet(viewsets.ModelViewSet):
"""
This API endpoint allows for the viewing of animal categories.
All categories include their direct subcategories and animals.
- To view a specific category, append the url with its [/id](/api/categories/1/).
- To get all top-level categories use [/main_groups](/api/categories/main_groups).
"""
def get_serializer_class(self):
if hasattr(self, 'action') and self.action == 'main_groups':
return MainGroupSerializer
return CategorySerializer
queryset = Category.objects.all().order_by('name')
queryset = CategorySerializer.eager_load(queryset)
serializer_class = get_serializer_class(super)
permission_classes = (IsAuthenticated, )
Question: how to logout the user when the logout button is triggered in django rest framework?