I've created a JWT-Authorised back end for an app. Login, logout, token retrieval and refresh all work fine, and as expected. I added a registration view this morning, which is throwing the usual "detail": "Authentication credentials were not provided.
error you'd expect for non-authenticated requests, as that's the default (see below).
However, because this is a registration endpoint, I don't want it to only allow authorised requests. (Having checked with a valid token, the rest of the view works as expected when you supply authentication.) Looking at the permissions section of the DRF docs, I thought that using the permission_classes wrapper with AllowAny would work here, but it hasn't.
What am I missing? I feel like the permission_classes decorator should override the default setting of 'IsAuthenticated'?
I'm testing on localhost from curl:
curl -X POST -H "Content-Type: application/json" -d '{"email":"[email protected]", "first_name": "boba", "last_name": "fett" "password":"xyz"}' http://localhost:8000/account/register/
View is:
@permission_classes(AllowAny)
@api_view(['POST'])
def register_user(request):
from django.contrib.auth.models import User
from rest_framework_jwt.views import obtain_jwt_token
if request.user.is_authenticated():
return Response ({"already_registered": "User with that username has already registered"}, status=status.HTTP_701_ALREADY_REGISTERED)
data = request.data
user, created = User.objects.get_or_create(username=data["email"],
email=data["email"],
first_name=data["first_name"],
last_name=data["last_name"],
password=data["password"])
if created:
token = obtain_jwt_token(data["email"],data["password"] )
return Response ({"token": token}, status=status.HTTP_200_OK)
else:
return Response ({"already_registered": "User with that username has already registered"}, status=status.HTTP_701_ALREADY_REGISTERED)
Permissions in settings.py are:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
}
Related questions: Django Rest Framework - Authentication credentials were not provided - I think the default permissions are correct, I just want to override them in this instance.
Django Rest Framework - DELETE ajax call failure due to incorrect CSFR token - CSRF not being used as JWT Based auth.
Django: Rest Framework authenticate header - Apache specific issue (currently still on devserver localhost)
Django Rest Framework Authentication credentials were not provided - Not yet answered!