Reputation: 27
I am using django-rest-knox, when I logout using knox_views.LogoutAllView.as_view()
, it gives me this error:
"detail": "Authentication credentials were not provided."
Note: I am using a custom user model(AbstarctUser
and BaseUserManager
)
Here is serializers.py
:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'email','birth_date','first_name','last_name')
# there is a registerserializer too
class LoginSerializer(serializers.Serializer):
email = serializers.EmailField()
password = serializers.CharField()
def validate(self, data):
user = authenticate(**data)
if user and user.is_active:
return user
raise serializers.ValidationError("Incorrect Credentials")
and here's views.py
:
class LoginView(generics.GenericAPIView):
serializer_class = LoginSerializer
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user= serializer.validated_data
return Response({
"user": UserSerializer(user, context=self.get_serializer_context()).data,
"token": AuthToken.objects.create(user)[1]
})
class RegisterAPI(generics.GenericAPIView):
serializer_class = RegisterSerializer
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.save()
return Response({
"user": UserSerializer(user, context=self.get_serializer_context()).data,
"token": AuthToken.objects.create(user)[1]
})
Upvotes: 1
Views: 674
Reputation: 1
you have to explicitly override authentication_classes on your new logout view or call it from Django setting
class MyCustomLogoutView(LogoutView)
authentication_classes = (Your custom Authentication,)
or
from rest_framework.settings import api_settings
class MyCustomLogoutView(LogoutView)
authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES
or access it from Django setting directly
Upvotes: 0
Reputation: 12078
From the docs:
This view accepts only a post request with an empty body. It responds to Knox Token Authentication. On a successful request, the token used to authenticate, and all other tokens registered to the same User account, are deleted from the system and can no longer be used to authenticate.
This means you still need to use the token you generated upon authentication with the logout endpoint.
Upvotes: 0